Why doesn't Application.DoEvents() allow the WebBrowser Control to process

0

I'm using C# with VS2012 to create an application containing a WebBrowser.

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:

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).

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:

// 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")

}

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?

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

c#
asked on Stack Overflow Feb 14, 2013 by Dan

1 Answer

0

To resolve this, I moved my "CloseWindow()" function to a Background Worker thread. This thread can then wait while the WebBrowser (on the GUI Thread) continues to process whatever it needs to process to return not busy.

answered on Stack Overflow Feb 15, 2013 by Dan

User contributions licensed under CC BY-SA 3.0