Why doesn't Application.DoEvents() allow the WebBrowser Control to process
<p>I'm using C# with VS2012 to create an application containing a WebBrowser.</p>

<p>After every [x] seconds of inactivity, I want my application to navigate back to its home page (This is designed as a terminal application). In principle, this is no problem, but I've stumbled upon an edge case:</p>

<p>If the user has clicked something that invokes, for example, a JavaScript MessageBox my browser gets stuck in a "Busy" state. The end result being that any attempts to navigate end up with a COM error (0x800700AA).</p>

<p>Using the user32.dll SendMessage function, I'm able to find and close the Window. Now, if my function ends at that point the WebBrowser carries on processing whatever script has invoked the window and eventually stops being busy. Here it is in pseudo code:</p>

<pre><code>// This works so long as there isn't a dialog 

private void NavigateHome(){

webBrowser.Navigate("http://www.google.com")

}

// This works to close the dialog and lets the browser return as not busy

private void NavigateHome(){

CloseWindows(); //Calls user32.dll

}

// This, again, works as long there isn't a dialog, otherwise the COM error returns

private void NavigateHome(){

CloseWindows(); //Calls user32.dll
webBrowser.Navigate("http://www.google.com")

}

// I thought this would be the solution, but the browser never continues processing

private void NavigateHome(){

CloseWindows(); //Calls user32.dll

while(webBrowser.isBusy){

Application.DoEvents();

}

webBrowser.Navigate("http://www.google.com")

}
</code></pre>

<p>Now, I guess the "proper" solution would be to watch the DocumentCompleted event after closing the window, BUT this doesn't feel very resilient. Ultimately, I don't really care about the document - I just want to return back to my original page. Has anyone got any ideas how should I proceed with this?</p>

<p>And I'd also like to know why Application.DoEvents() doesn't work like I expected.</p>
