我有一个WebBrowser控件,我告诉他导航到几个网页。对于一个导航,下面的代码工作正常。当代码片段2被执行时,我得到COM object that has been separated from its underlying RCW cannot be used.
错误。我假设它是因为我的网络浏览器正在处理或者有线程的东西,但它是一个全局变量!我应该可以重复使用它!所以我只想访问几个网页并保留状态,我需要使用WebBrowser类。
//browser is a global
browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
// code piece 1
Thread t = new Thread(() =>
{
browser.Navigate(MAIN_URI);
Application.Run();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
// code piece 2
Thread t = new Thread(() =>
{
browser.Navigate(DIFFERENT_URI);
Application.Run();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
// code piece 3
private static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedArgs args)
{
// getting document data and stuff
Application.Exit();
}