C#System.InvalidCastException [英] C# System.InvalidCastException

查看:207
本文介绍了C#System.InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  System.InvalidCastException未被用户代码
消息=指定的转换无效。
Source = System.Windows.Forms
StackTrace:
在System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
在System.Windows.Forms.WebBrowser.get_Document( )
在System.Windows.Forms.WebBrowser.get_DocumentStream()
在System.Windows.Forms.WebBrowser.get_DocumentText()
在SiteBot.MainWindow.backgroundWorker1_DoWork(对象发件人,DoWorkEventArgs e)在D:\Documents\Visual Studio 2010\Projects\SiteBot\MainWindow.cs:line 35
在System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
在System.ComponentModel。 BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:


解决方案

p>以下解决了你的十字线问题。

  public delegate string GetStringHandler(); 
public string GetDocumentText()
{
if(InvokeRequired)
返回Invoke(新的GetStringHandler(GetDocumentText))作为字符串;
else
return webBrowser.DocumentText;
}

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


Why I'm getting this error?

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: 

解决方案

The following solves your cross thread issue.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆