web浏览器文档投无效浏览器、文档、web

2023-09-02 02:05:36 作者:[承蒙时光不弃]

我有我的网页上WebBrowser控件。火灾在浏览器控件中显示的网页上一些JavaScript,我呼吁this.myBrowser.Document.InvokeScript(测试);

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");

这适用于我的IE7的dev的盒子,而是一个单独的测试机上,我得到了以下错误:

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:DevelopTestProjectfrmMain.cs:line 265
   at MyTest.frmMain.prtCardReader_DataReceived(Object sender, SerialDataReceivedEventArgs e) in E:DevelopTestProjectfrmMain.cs:line 355

该试验机正在运行IE6。虽然我不知道,我猜测,在IE浏览器版本的差异可能是错误的原因。这是否健全的权利?我该如何解决此问题?

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?

frmMain.cs的265行是:

Line 265 of frmMain.cs is:

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

frmMain.cs的355行是:

Line 355 of frmMain.cs is:

CardRead(strCardData_m);

我觉得异常被抛出的WebBrowser对象的文档属性调用。

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

推荐答案

想通了。我的电话是被从串行口接收数据事件,不主界面线程上执行调用。我用了一个this.Invoke()方法调用,让我的code到主界面线程上执行,现在一切都很好!

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!