Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is WebBrowser_DocumentCompleted() firing twice?

Well, I'm using a simple webbrowser control to browse to a page, so I need to change the Text of the form while doing so. I'm using -

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

but using a breakpoint, i noticed that, this event is firing twice. I even tried _Navigated() event. it also fired twice. Resulting the title to "Webber - google.co.in - google.co.in" ..

I also noticed that this event fired several times while loading msn.com.. I'm trying to change the text of the form only when the page has finished loading totally..

Any remedy?

like image 293
Bibhas Debnath Avatar asked Feb 24 '10 19:02

Bibhas Debnath


2 Answers

You can check the WebBrowser.ReadyState when the event is fired:

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

ReadyState will be set to Complete once the whole document is ready.

like image 162
Krassi Avatar answered Oct 02 '22 20:10

Krassi


Every time a frame loads, the event is fired.

Also, before you even go there, the IsBusy property will only be True whilst the first frame has not loaded.

void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
    return; 

  //The page is finished loading 
}
like image 43
Kyle Rosendo Avatar answered Oct 02 '22 20:10

Kyle Rosendo