Alexsandro
Alexsandro

Reputation: 1271

Why do I get a run-time error when I run a WebBrowser control on another thread?

Unhandled exception at 0x0e90a1c0 in MyApp.exe: 0xC0000005: Access violation.

Based in the post How might I create and use a WebBrowser control on a worker thread? I'm trying to run the WebPagePump class on another thread.

for (int i = 0; i < 5; i++)
{
    Thread t = new Thread(delegate() { WebNav1(); });
    t.Start();
}

private Action WebNav1 = delegate()
{
    WebPagePump a = new WebPagePump();
    a.Navigate(new Uri("http://www.mywebsite.com"));
    a.Completed += delegate(WebBrowser wb)
        {
            Console.WriteLine("It's loaded!");
            a.Dispose();
        };
};

Upvotes: 1

Views: 501

Answers (1)

Nick Butler
Nick Butler

Reputation: 24383

You need to specify a Single Threaded Apartment:

Thread t = new Thread(delegate() { WebNav1(); });
t.ApartmentState = ApartmentState.STA;
t.Start();

Upvotes: 2

Related Questions