WebBrowser.DocumentComplete Firing Multiple Times: Solution

Software

My previous hunch was correct: before you do any actual work in the DocumentComplete event handler, make sure the ReadyState of the control equals “Complete”.

Here’s the updated diagnostic output: notice the time taken drops to three seconds now that I’m not looping through the worker code eleven different times.

'Snapper.WinForm.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Uninitialized
Uninitialized
Uninitialized
Uninitialized
Uninitialized
'Snapper.WinForm.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll'
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
12/29/2007 1:15:03 PM - DocumentCompleted!
12/29/2007 1:15:03 PM - DocumentCompleted!
Interactive
Interactive
Interactive
12/29/2007 1:15:03 PM - DocumentCompleted!
12/29/2007 1:15:03 PM - DocumentCompleted!
Interactive
Interactive
Interactive
Interactive
Interactive
12/29/2007 1:15:05 PM - DocumentCompleted!
12/29/2007 1:15:05 PM - DocumentCompleted!
Interactive
Interactive
Interactive
Interactive
Interactive
Interactive
12/29/2007 1:15:05 PM - DocumentCompleted!
12/29/2007 1:15:05 PM - DocumentCompleted!
12/29/2007 1:15:05 PM - DocumentCompleted!
12/29/2007 1:15:05 PM - DocumentCompleted!
Interactive
12/29/2007 1:15:06 PM - DocumentCompleted!
Complete
12/29/2007 1:15:06 PM - DocumentCompleted!
Doing work..

It’s probably also the case that the WebBrowserDocumentCompletedEventArgs parameter of the DocumentCompleted event handler gives me additional info with which to discriminate this ReadyState. May have to look into that one.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Furl
  • Ma.gnolia
  • Reddit
  • TwitThis
6 Comments

6 Comments

  1. Frequency  •  Feb 23, 2008 @10:22 pm

    Great solution, thanks a lot bro.

  2. Dotmad  •  Mar 29, 2008 @12:21 pm
  3. Confused About ReadyState  •  Aug 19, 2008 @11:35 am

    I’m trying to get the HTML DOM. So, I am using the WebBrowser control to navigate to a URI, then looping until ReadyState is Complete. The code below doesn’t seem to work. Any ideas why?

    Dim MyWebBrowser As New WebBrowser
    MyWebBrowser.Navigate(”http://www.msn.com/”) ‘ Apparently, this does nothing

    ‘ Loops infinitely
    While MyWebBrowser.ReadyState WebBrowserReadyState.Complete
    End While

    ‘ Ultimately, this is what I really need
    Dim MyHtmlDocument As HtmlDocument = MyWebBrowser.Document

  4. shahriar khazaie  •  Mar 25, 2009 @10:19 am

    your WebBrowser Control should be visible in a form to work truly.
    so the command “Dim MyWebBrowser As New WebBrowser” does not work .

  5. anthonyrstevens  •  Mar 25, 2009 @7:40 pm

    Hm…I don’t think so. It has to run in a UI thread, but it doesn’t necessarily have to be in a WinForm.

  6. shahriar khazaie (360.yahoo.com/shahriarplus)  •  Apr 18, 2009 @12:04 pm

    I want notice that when you disable the IE Javascript (ActiveScripting) , IE fires DocumentComplete one time actually.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">