Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the .net webBrowser object

Tags:

browser

c#

.net

any one know of a tutorial for using the System.Windows.Forms.WebBrowser object? Had a look around but can't find one. The code I have so far is (the very complex):

System.Windows.Forms.WebBrowser b = new System.Windows.Forms.WebBrowser();
b.Navigate("http://www.google.co.uk");

but it doesn't actually navigate anywhere (i.e. b.Url is null, b.Document is null etc)

Thanks

like image 321
Patrick Avatar asked Nov 05 '22 23:11

Patrick


1 Answers

It takes time for the browser to navigate to a page. The Navigate() method does not block until the navigation is complete, that would freeze the user interface. The DocumentCompleted event is fired when it's done. You have to move your code into an event handler for that event.

An additional requirement is that the thread on which you create a WB is a happy home for single-threaded COM components. It must be STA and pump a message loop. A console mode app does not meet this requirement, only a Winforms or WPF project has such a thread. Check this answer for a solution that's compatible with console mode programs.

like image 61
Hans Passant Avatar answered Nov 09 '22 12:11

Hans Passant