日期:2014-05-17  浏览次数:21113 次

C#定义一个线程去获取窗体控件webBrowser1里的元素
th = new Thread(ab);
th.IsBackground = true; //线程后运行  
th.Start(); //启动线程 
public void ab()
{
            HtmlElement check = webBrowser1.Document.GetElementById("checkout-now").InvokeMember("click");
}
每次执行到ab()里显示,制定的转换无效。请问是什么原因??谁能告诉我?

未处理 System.InvalidCastException
  Message="指定的转换无效。"
  Source="System.Windows.Forms"
  StackTrace:
       在 System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
       在 System.Windows.Forms.WebBrowser.get_Document()
       在 苹果自动订货.Form1.ab() 位置 C:\Users\DotA\Desktop\11\苹果自动订货\苹果自动订货\Form1.cs:行号 44
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:

------解决方案--------------------
看错误说明好像在webBrowser1.Document获取Document的时候就出错了,可以加断点看下具体情况
------解决方案--------------------
 object check = webBrowser1.Document.GetElementById("checkout-now").InvokeMember("click");
------解决方案--------------------
引用:
object check = webBrowser1.Document.GetElementById("checkout-now").InvokeMember("click");


+1
------解决方案--------------------
别放到线程中,放到时间控件中,如果用线程就用消息的方式处理。
因为线程直接访问UI组件是会出错的。