C# System.InvalidCastException

标签 c# .net exception

为什么我会收到此错误?

enter image description here

System.InvalidCastException was unhandled by user code
  Message=Specified cast is not valid.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
       at System.Windows.Forms.WebBrowser.get_Document()
       at System.Windows.Forms.WebBrowser.get_DocumentStream()
       at System.Windows.Forms.WebBrowser.get_DocumentText()
       at SiteBot.MainWindow.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\Documents\Visual Studio 2010\Projects\SiteBot\MainWindow.cs:line 35
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException: 

最佳答案

以下内容解决了您的跨线程问题。

public delegate string GetStringHandler();
public string GetDocumentText()
{
    if (InvokeRequired)
        return Invoke(new GetStringHandler(GetDocumentText)) as string;
    else
        return webBrowser.DocumentText;
}

if (regAddId.IsMatch(GetDocumentText()))
{
}

关于C# System.InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41104826/

相关文章:

c# - DOT 语言解析器

c# - 统一构建错误

c# - C# 垃圾收集器如何找到唯一引用是内部指针的对象?

c# - 使用 SemanticResultKey 时出现 TargetInvocationException

c# - 如何更新 mongodb 中除指定字段之外的所有文档字段

c# - CLI/C++ 和 C# : How to capture C++ exceptions in Release Mode

java - 将元数据添加到 java throwable 对象

c# - 按 namespace 对列表进行排序

c# - Windows 8 上 App.Config 的路径访问被拒绝。如何更新 App.Config?

c# - 如何记录异常信息以进行故障排除?