Friday, May 25, 2007

WebBrowser.DocumentCompleted is raised more than once

I'm trying to write several utilities to query web sites, so I decided on using the WebBrowser.DocumentText property to get the page content.
As it turns out, unlike Microsoft's documentation, the DocumentCompleted event is raised several times for most sites. It was triggered 8 times for this blog.
If this page is an example, the first load is enough regarding content, so I'm using the Interlocked class to perform the event's code only the first time the event was triggered.

Update: Turns out there is a solution to the problem.

4 comments:

Anonymous said...

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("" + e.Url);
}
e.Url contians the document completed you put the check of your destination URL like in your case
if(e.Url == "http://dotmad.blogspot.com/2007/05/webbrowserdocumentcompleted-is-raised.html" )
{
}

Adi said...

Thanks for the tip, but I tested it (writing the URL to a listbox inside the event) and I got two hits in which the URL was identical.

Marcus Erickson said...

The way to do this is to watch the WebBrowser controls's ReadyState property. When it is really completely loaded, the ReadyState will be set to Complete.

Anonymous said...

nhà cái 138bet hấp dẫn với hàng ngàn khuyến mãi