当我在另一个线程上运行WebBrowser控件时,为什么会出现运行时错误?

时间:2012-01-17 12:20:04

标签: c# multithreading browser

MyApp.exe中0x0e90a1c0处的未处理异常:0xC0000005:访问冲突。

基于帖子How might I create and use a WebBrowser control on a worker thread?我试图在另一个帖子上运行WebPagePump类。

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();
        };
};

1 个答案:

答案 0 :(得分:2)

您需要指定单线程公寓:

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