简体   繁体   English

C#WebBrowser控件未触发DocumentCompleted事件

[英]C# WebBrowser control not firing the DocumentCompleted event

I have a program that is using the C# WebBrowser control and it needs to detect which page is loaded to determine what to do next. 我有一个使用C#WebBrowser控件的程序,它需要检测加载哪个页面以确定下一步该做什么。 The program works fine on most of the employee's computers at the office, but it does not work on some. 该程序在办公室的大多数员工的计算机上运行良好,但在某些计算机上不起作用。

I have determined the problem is that the documentCompleted event is not firing on those computers it does not work on. 我已经确定问题是documentCompleted事件没有在那些无法工作的计算机上触发。

This program does use threads to process data, and the webbrowser.navigate call is made through a delegate. 该程序使用线程处理数据, webbrowser.navigate调用是通过委托进行的。 But I have also changed the code to not use delegates for the navigate action with no change in the result. 但是我也将代码更改为不使用委托进行导航操作而不改变结果。

I also made a separate program that has just a basic WebBrowser and debug textfield , and the DocumentCompleted event does fire when using that program. 我还创建了一个单独的程序,它只有一个基本的WebBrowser和调试textfield ,而DocumentCompleted事件在使用该程序时触发。

NOTE: The computers that it is not firing on are older PCs with single core/thread processors. 注意:未启动的计算机是具有单核/线程处理器的旧PC。

I am out of ideas on this one, any help would be appreciated. 我对这一点缺乏想法,任何帮助都将不胜感激。

As explained by CodeBlock, this seems to be related by the installation state of Microsoft.mshtml.dll 正如CodeBlock所解释的那样,这似乎与Microsoft.mshtml.dll的安装状态有关

We've got customers where the Microsoft.mshtml.dll is not present in GAC (nor in computer), and then the WebBrowser component never fires any event. 我们有客户在GAC(也不是计算机)中没有Microsoft.mshtml.dll ,然后WebBrowser组件永远不会触发任何事件。

By using Reflector in the WebBrowser class, the DocumentComplete event is raised by a subclass named WebBrowserEvent , which implement a private interface DWebBrowserEvents2 . 通过在WebBrowser类中使用Reflector, DocumentComplete事件由名为WebBrowserEvent的子类引发,该子类实现私有接口DWebBrowserEvents2

This interface is a ComImport of {34A715A0-6587-11D0-924A-0020AFC7AC4D} , which, I suppose, is related to Microsoft.mshtml.dll. 此接口是{34A715A0-6587-11D0-924A-0020AFC7AC4D}ComImport ,我想这与Microsoft.mshtml.dll有关。

So our solution was to install the Office 2003 Redistributable Primary Interop Assemblies , which install the DLL on Program Files then register it on the GAC. 所以我们的解决方案是安装Office 2003 Redistributable Primary Interop Assemblies ,它在Program Files上安装DLL然后在GAC上注册它。

Note : Don't pay attention to the .NET Framework 1.1 required or office required, it just copies some dlls. 注意:不要注意所需的.NET Framework 1.1或办公室,它只是复制一些dll。

Note 2 : The 2007 package seems to include the same dll. 注2: 2007包似乎包含相同的dll。

@Pavel L: @Pavel L:

The problem here is you used a web browser control from mshtml.dll but .NET framework does not include this file. 这里的问题是您使用mshtml.dll的Web浏览器控件,但.NET框架不包含此文件。 The solution for this is simply copy mshtml.dll to your app dir or set 'Copy local' property of Microsoft.mshtml to True. 解决方法是将mshtml.dll复制到您的应用程序目录或将Microsoft.mshtml的“复制本地”属性设置为True。

Sorry for my bad english :D 抱歉我的英文不好:D

While trying to solve this question , I originally had my WebBrowser control in a using() block, and it was disposing itself before the DocumentCompleted event could fire. 在尝试解决这个问题时 ,我最初将我的WebBrowser控件放在using()块中,并且在DocumentCompleted事件触发之前它处理了自己。 Are you doing something similar, or is the WebBrowser on a form that's staying open? 你在做类似的事情,还是WebBrowser在一个保持开放的形式?

If it is a threading issue, make sure you are calling Application.DoEvents(). 如果是线程问题,请确保调用Application.DoEvents()。 I've had problems with WebBrowser not working right when I failed to do that. 当我没有做到这一点时,我遇到了WebBrowser无法正常工作的问题。

Well I'd like to share even more simple solution rather than installing a package. 好吧,我想分享更简单的解决方案而不是安装包。

As it was stated before the DocumentComplete event somehow depends on Microsoft.mshtml.dll file. 正如之前所述,DocumentComplete事件以某种方式依赖于Microsoft.mshtml.dll文件。 You can find it on a machine with Visual Studio installed. 您可以在安装了Visual Studio的计算机上找到它。 So it needs to be copied and installed to the target machine. 因此需要将其复制并安装到目标计算机上。 Here's the description of the process: 以下是该过程的描述:

  1. Place the Microsoft.mshtml.dll file into the "%ProgramFiles%\\Microsoft.NET\\Primary Interop Assemblies" folder. 将Microsoft.mshtml.dll文件放入“%ProgramFiles%\\ Microsoft.NET \\ Primary Interop Assemblies”文件夹中。
  2. Then drag and drop it into the "%SystemRoot%\\Assembly" folder. 然后将其拖放到“%SystemRoot%\\ Assembly”文件夹中。
  3. Register the library with RegAsm.exe utility (which is located in the appropriate subfolder of "%windir%\\Microsoft.NET\\Framework"). 使用RegAsm.exe实用程序(位于“%windir%\\ Microsoft.NET \\ Framework”的相应子文件夹中)注册库。

PS May be it may be done in a better way but this solution works and I hope it'll be useful for you. PS可能是以更好的方式完成,但这个解决方案有效,我希望它对你有用。

In order for the documentCompleted-Event to fire, 为了使documentCompleted-Event能够触发,
the visible property of the web browser needs to be set to true. Web浏览器的visible属性需要设置为true。

It can also be on visible, if you have multiple screens. 如果您有多个屏幕,它也可以是可见的。
If you have > 1 screen, switch off all but the main screen, this should resolve the problem. 如果您有> 1个屏幕,请关闭除主屏幕以外的所有屏幕,这样可以解决问题。
Alternatively, move the window to the 2nd screen. 或者,将窗口移动到第二个屏幕。

Make sure webbrowser.Visible=true; 确保webbrowser.Visible=true; this works for me, I had similar problems previously. 这对我有用,我之前遇到过类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM