"指定的转换是无效的"错误 [英] "Specified cast is not valid" error
本文介绍了"指定的转换是无效的"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用这个code这是为了检查webBrowser1文本,而不是虽然我得到错误指定强制转换是无效的。为字符串docText = webBrowser1.Document.Body.InnerText;
。任何想法,为什么?难道是因为我从另一个线程访问web浏览器?谢谢你。
公共Form1中()
{
的InitializeComponent();
}私人无效Form1_Load的(对象发件人,EventArgs的发送)
{
}私人无效backgroundWorker1_DoWork(对象发件人,DoWorkEventArgs E)
{
字符串docText = webBrowser1.Document.Body.InnerText; 如果(docText ==你好)
{
MessageBox.Show(惊动!);
}
}私人无效timer1_Tick(对象发件人,EventArgs的发送)
{
backgroundWorker1.RunWorkerAsync();
}
解决方案
我会尝试与...
backgroundWorker1.RunWorkerAsync(webBrowser1.Document.Body.InnerText);
这将消除转换异常
和在DoWork的
字符串docText = e.Argument.ToString();
这将删除UI线程问题
I'm using this code which is meant to check the text in webBrowser1, although instead I'm getting the error "Specified cast is not valid." for string docText = webBrowser1.Document.Body.InnerText;
. Any ideas why? Could it be because I'm accessing the webBrowser from another thread? Thanks.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string docText = webBrowser1.Document.Body.InnerText;
if (docText == "Hello")
{
MessageBox.Show("Alerted!");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
解决方案
I will try with...
backgroundWorker1.RunWorkerAsync(webBrowser1.Document.Body.InnerText);
this will remove the cast exception
and in DoWork
string docText = e.Argument.ToString();
this will remove the UI thread issue
这篇关于"指定的转换是无效的"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文