Why is WebBrowser_DocumentCompleted () run twice?

Well, I use a simple web browser control to go to the page, so I need to change the text of the form during this. I use -

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { this.Text += " - " + webBrowser1.Document.Domain; } 

but using a breakpoint, I noticed that this event fires twice. I even tried the _Navigated() event. he also fired twice. Calling the header "Webber - google.co.in - google.co.in" ..

I also noticed that this event was triggered several times when msn.com was loading. I try to change the text of the form only when the page has fully loaded.

Any remedy?

+17
c # browser navigation
Feb 24 '10 at 19:26
source share
9 answers

You can check WebBrowser.ReadyState when the event fires:

 if (browser.ReadyState != WebBrowserReadyState.Complete) return; 

ReadyState will be installed to complete as soon as the entire document is ready.

+26
Mar 09 '10 at 15:49
source share

Each time a frame is loaded, an event is fired.

Also, before you enter there, the IsBusy property will only be True until the first frame has been loaded.

Try this :

 void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath) return; //The page is finished loading } 
+17
Feb 25 '10 at 5:24
source share

It runs once per frame.

+3
Feb 25 '10 at 5:20
source share

I have the same problem, and the reason is that by default, when you add a control, it generates such designer code.

 this.webBrowser1.Url = new System.Uri("", System.UriKind.Relative); 

and if you change the url after calling

 InitializeComponent(); WebBrowser.Navigate("NewUrl.com"); 

It will load two different pages: A : Blank and NewUrl.com

Just delete the designer code ... and you will stop the "double" event.

+3
Oct. 14 '10 at 13:03
source share

If shooting twice is a problem, then this should work:

  string body=""; private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (body == webBrowser1.Document.Body.InnerHtml) return; body = webBrowser1.Document.Body.InnerHtml; // Here is something you want } 
+3
Oct 24 '13 at 20:17
source share

You may have signed this event several times, as in your method, when you go to the URL every time you sign this event.

To solve this problem, move this line from the method and place it somewhere else where it will be called only once per instance. Perhaps in the class constructor ... This should solve your problem.

+1
Mar 13 '13 at 11:27
source share

How to determine when a page will be loaded Loading into WebBrowser Control DocumentCompleted is the WinForms wrapper for evert DocumentComplete, however WebBrowserDocumentCompletedEventArgs hides the sender parameter, so you cannot determine which frame raises the event. Alternatively, you can check WebBrowser.ReadyState .

0
Mar 01 '10 at 20:52
source share

Actually, this does not always work. I do not understand why not. I have a timer and check ReadyState several times for a few minutes. (Using the built-in browser control).

0
Mar 09 '10 at 21:37
source share

if (browser.ReadyState != WebBrowserReadyState.Complete) .

And when there are frames on the page, DocumentCompleted will be launched several times. And it's hard to solve. Some methods, like checking URLs, are inaccurate.

By the way, why not use this:

this.Text = stringA + " - " + webBrowser1.Document.Domain;

Try using a fixed prefix, the problem can be solved easily.

0
Mar 19 '12 at 9:29
source share



All Articles