Why is WebBrowser_DocumentCompleted() firing twice?
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.
DocumentCompleted firing multiple times - accepted StackOverflow answer not working
DocumentComplete
may get fired multiple times for many reasons (frames, ajax, etc). At the same time, for a particular document, window.onload
event will be fired only once. So, perhaps, you can do your processing upon window.onload
. I just answered a related question on how that can be done.
WebBrowser Document Completed Event C#
As I recall, DocumentCompleted will fire multiple times if the document being navigated to has iframes that embed other web pages.
If you only want to receive the event exactly once, just unsubscribe from the DocumentCompleted handler:
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
MessageBox.Show(webBrowser.Url.ToString() );
}
private void navBtnClick(object sender, EventArgs e)
{
var wbrowser = new WebBrowser();
wbrowser.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
wbrowser.Navigate("http://www.google.com");
}
Alternately, you can use System.IObservable and ReactiveExtensions to subscribe to exactly one event firing:
private void navBtnClick(object sender, EventArgs e)
{
var browser = new WebBrowser();
var docCompleted = Observable.FromEventPattern<WebBrowserDocumentCompletedEventArgs>(browser, "DocumentCompleted")
docCompleted
.Take(1) // Take only one event firing
.Subscribe(i => MessageBox.Show(browser.Url.ToString()));
browser.Navigate("http://www.google.com");
}
Detect WebBrowser complete page loading
I think the DocumentCompleted
event will get fired for all child documents that are loaded as well (like JS and CSS, for example). You could look at the WebBrowserDocumentCompletedEventArgs
in DocumentCompleted
and check the Url property and compare that to the Url of the main page.
Related Topics
Specified Key Is Not a Valid Size for This Algorithm
Mocking Static Methods Using Rhino.Mocks
How to Scroll to Element in Uwp
Should You Access a Variable Within the Same Class via a Property
How to Implement the Sieve of Eratosthenes Using Multithreaded C#
Why Would Try/Finally Rather Than a "Using" Statement Help Avoid a Race Condition
What Would Be the Fastest Way to Concatenate Three Files in C#
How Can User Resize Control at Runtime in Winforms
Combine Two Linq Lambda Expressions
How to Disable Aero Snap in an Application
Cannot Deserialize the Current JSON Array (E.G. [1,2,3])
How to Handle Forms Authentication Timeout Exceptions in ASP.NET
Opening a "Known File Type" into Running Instance of Custom App - .Net
Object' Does Not Contain a Definition for 'X'
How to Call a SQL Stored Procedure Using Entityframework 7 and ASP.NET 5