Invalid WebBrowser Document

I have a WebBrowser control on my page. To run javascript on the page displayed in the browser control, I call this.myBrowser.Document.InvokeScript ("Test");

This works in my dev block with IE7, but on a separate 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 

IE6 is running on the test computer. Although I'm not sure, I assume that the cause of the errors is the difference in versions of IE. Does this sound right? How do I get around this?

Line 265 of the frmMain.cs file:

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

Line 355 of the frmMain.cs file:

 CardRead(strCardData_m); 

I think the exception is thrown by calling the Document property of the WebBrowser object.

+1
source share
1 answer

Figured it out. My call was called from a received serial port data event that is not being executed in the main GUI thread. I used the this.Invoke () method to get my code to execute in the main GUI thread, now everything is fine!

+8
source

Source: https://habr.com/ru/post/1302813/


All Articles