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/5268281/

相关文章:

c# - 缺少 IsNullOrEmptyOrWhiteSpace 方法

c# - Entity Framework Core 相关数据

c# - 异常在异步代码块中被忽略

c# - Util.CreateAppDomain 在 LINQPad 中失败

c# - 如何从 C# 的 BinaryWriter 创建的二进制文件中读取 php 中的字符串

.net - 多文档应用程序的 MVVM 设计?

c# - 如何让窗口出现在任务栏中?

javascript - 将短语表示为代码块

android - Android ViewDragHelper 中的 ArrayIndexOutOfBoundsException

java - 如何解决错误 "java.lang.IllegalArgumentException: Can' t 初始化已配置的调试器!”在 smack 中