user42931
user42931

Reputation: 1165

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. 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.

This program does use threads to process data, and the webbrowser.navigate call is made through a delegate. 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.

NOTE: The computers that it is not firing on are older PCs with single core/thread processors.

I am out of ideas on this one, any help would be appreciated.

Upvotes: 3

Views: 17718

Answers (6)

Reno
Reno

Reputation: 113

Make sure webbrowser.Visible=true; this works for me, I had similar problems previously.

Upvotes: 0

Stefan Steiger
Stefan Steiger

Reputation: 82276

In order for the documentCompleted-Event to fire,
the visible property of the web browser needs to be set to 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.
Alternatively, move the window to the 2nd screen.

Upvotes: 1

Pavel L.
Pavel L.

Reputation: 11

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. You can find it on a machine with Visual Studio installed. 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.
  2. Then drag and drop it into the "%SystemRoot%\Assembly" folder.
  3. Register the library with RegAsm.exe utility (which is located in the appropriate subfolder of "%windir%\Microsoft.NET\Framework").

P.S. May be it may be done in a better way but this solution works and I hope it'll be useful for you.

Upvotes: 1

tbolon
tbolon

Reputation: 628

As explained by CodeBlock, this seems to be related by the installation state of 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.

By using Reflector in the WebBrowser class, the DocumentComplete event is raised by a subclass named WebBrowserEvent, which implement a private interface DWebBrowserEvents2.

This interface is a ComImport of {34A715A0-6587-11D0-924A-0020AFC7AC4D}, which, I suppose, is related to 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.

Note : Don't pay attention to the .NET Framework 1.1 required or office required, it just copies some dlls.

Note 2 : The 2007 package seems to include the same dll.

Upvotes: 6

CodeBlock
CodeBlock

Reputation: 51

@Pavel L:

The problem here is you used a web browser control from mshtml.dll but .NET framework does not include this file. The solution for this is simply copy mshtml.dll to your app dir or set 'Copy local' property of Microsoft.mshtml to True.

Sorry for my bad english :D

Upvotes: 5

Erich Mirabal
Erich Mirabal

Reputation: 10048

If it is a threading issue, make sure you are calling Application.DoEvents(). I've had problems with WebBrowser not working right when I failed to do that.

Upvotes: 2

Related Questions