1

I want to login programmatically to another website by using webbrowser object so I have a webbrowser object in my project. The problem is Eachtime navigating it to any address, its "document" property remains null all the time and so the DocumentCompleted event never fires. The sample code is as below. Could you give any suggestions? Thanks in advance.

    private WebBrowser browser;

    protected void Page_Load(object sender, EventArgs e)
    {
        var t = new Thread(CreateBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

    }

    private void CreateBrowser()
    {
        browser = new WebBrowser();
        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
        browser.Navigate("http://www.google.com");

    }
noseratio
  • 59,932
  • 34
  • 208
  • 486
diyapesen
  • 33
  • 2
  • 8

1 Answers1

0

You need a message loop on the new thread where you create an instance of WebBrowser, it's essential for WebBrowser control to work properly and fire events. That's what Application.Run does. Here's a great example of how it can be done.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486