0

I need to prevent WebBrowser.Navigate() run on the same thread because I need the process to be certain order.

I came across Async/Await implementation of WebBrowser class for .NET which uses async/await implementation but I have to use .net 3.5 which doesn't support it.

Anyone know of any alternative methods?

Community
  • 1
  • 1
  • 1
    You cannot prevent it from running on the same thread, WebBrowser is a single-threaded COM object. The simple alternative to async/await is a state machine. Or run everything on a separate thread, as [shown here](http://stackoverflow.com/a/4271581/17034). – Hans Passant Apr 24 '14 at 22:39
  • If you can't use `async`/`await`, perhaps you can use `IEnumerable`/`yield`: http://stackoverflow.com/a/22296644/1768303 – noseratio Apr 24 '14 at 23:24

2 Answers2

0

As far as I understand your requirements, you can use the WebBrowser component's DocumentCompleted event in the following way:

webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Loaded);
webBrowser.Navigate(...);

...

void Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // do stuff that needs to be run after the page is loaded
}
qqbenq
  • 10,220
  • 4
  • 40
  • 45
0

I ended up setting a flag on the main form. Then there is a thread.sleep until the flag is reset.