Using WebBrowser in a console application

Right idea, wrong execution. The WebBrowser.Navigate() only tells the web browser to start navigating to the web page you asked for. That takes time, hundreds of milliseconds typically. Internet Explorer internally starts threads to get the job done. It tells you when it is done by raising the DocumentCompleted event. You don’t wait for that so that’s crash city first.

Next problem is that the DocumentCompleted event won’t be raised in your code. You have to honor the STA contract, it requires you to pump a message loop. That’s the all-mighty way that a background thread, like the one that IE uses to retrieve a web page, tells your thread that the job is done.

The boilerplate code you need is available in this answer.

Leave a Comment