1

I'm trying to use this control inside a class library, but when I run the code below, I did not see a request to google being sent (using fiddler).

public class WebBrowserTest
{
    public WebBrowserTest()
    {
        var t = new Thread(StartBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

    private void StartBrowser()
    {
        WebBrowser web;
        web = new WebBrowser();
        web.Navigate("http://www.google.com");
    }
}

My guess is this has something to do with threading, and possibly the thread ending before the control gets a chance to send the request. But I have no idea where to start with solving this.

THE SOLUTION

I found this solution to work, the events are getting fired and the main thread waits for the STA thread.

public class WebThread
{
    private WebBrowser web { get; set; }

    public void StartBrowser()
    {
        web = new WebBrowser();
        web.Visible = true;
        web.DocumentCompleted += Web_DocumentCompleted;
        web.ScriptErrorsSuppressed = true;
        web.Navigate("http://www.google.com");

        Application.Run();

        web.Dispose();
    }

    private void Web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Debug.Print("Arrived: " + e.Url.ToString());

        if (e.Url.ToString() == "http://www.google.com.au/")
        {
            Application.ExitThread();
        }
    }
}


public class WebBrowserTest
{
    public WebBrowserTest()
    {
        Debug.Print("Thread is starting.");
        var webThread = new WebThread();

        var t = new Thread(webThread.StartBrowser);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

        while(t.IsAlive)
        {
            Thread.Sleep(5000);
        }

        Debug.Print("Thread has finished.");
    }
}
g.foley
  • 2,152
  • 3
  • 19
  • 27
  • Think about what happens to the variable web when the thread ends. – user957902 Oct 22 '11 at 02:43
  • 1
    UI controls are only possible in the main foreground thread; I'm not sure how it behaves when you create it in a background thread. – Uwe Keim Oct 22 '11 at 09:07
  • possible duplicate of [C# WebBrowser Control in a new thread](http://stackoverflow.com/questions/4269800/c-sharp-webbrowser-control-in-a-new-thread) – Hans Passant Oct 22 '11 at 09:31
  • Note a subtle problem with the code: We start a navigation to google.com but then a redirect send us to the local Google service. In the case of the OP it presumbly was www.google.com.au, but in your case it can be another url (e.g. https://www.google.co.il/?...) and the code will just hang since the thread will never exit. – Dror Mar 28 '14 at 18:33

1 Answers1

2

WebBrowser.Navigate( ... ) doesn't block - it returns immediately, before the request is sent. Since your thread function then exits, your whole thread ends and takes your WebBrowser control with it.

If you're just trying to download a web page, have a look at the WebClient class. It has many async methods which means you probably won't even have to create your own thread.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • WebClient doesn't work in this case since I need the javascript to be executed. But your answer was the correct one. I have added my solution below. – g.foley Oct 23 '11 at 02:04