WebBrowser document cast not valid

只谈情不闲聊 提交于 2019-11-27 07:26:14

问题


I've got a WebBrowser control on my page. To fire some javascript on a page displayed in the browser control, I call this.myBrowser.Document.InvokeScript("Test");

This works on my dev box with IE7, but on a seperate test machine I get the following error:

Exception Type: System.InvalidCastException
Message: Specified cast is not valid.
Source: System.Windows.Forms
Stack Trace: 
   at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
   at System.Windows.Forms.WebBrowser.get_Document()
   at InteriorHealth.EmbeddedBrowser.frmMain.CardRead(String strData) in E:\Develop\TestProject\frmMain.cs:line 265
   at MyTest.frmMain.prtCardReader_DataReceived(Object sender, SerialDataReceivedEventArgs e) in E:\Develop\TestProject\frmMain.cs:line 355

The test machine is running IE6. Although I'm not sure, I'm speculating that the difference in IE versions may be the reason for the error. Does this sound right? How do I work around this?

Line 265 of frmMain.cs is:

 this.webKiosk.Document.InvokeScript(ConfigurationState.CardReader.Error.FireJavaScriptMethod);

Line 355 of frmMain.cs is:

CardRead(strCardData_m);

I think the exception is being thrown by the Document property call on the WebBrowser object.


回答1:


Figured it out. My call was being invoked from a serial port data received event, which doesn't execute on the main GUI thread. I used a this.Invoke() method call to get my code to execute on the main GUI thread, all is good now!



来源:https://stackoverflow.com/questions/315938/webbrowser-document-cast-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!