I have a WebBrowser control that I'm telling to navigate to a couple webpages. My code below works fine, for one navigation. When code piece 2 gets executed, I get the COM object that has been separated from its underlying RCW cannot be used.
error. I'm assuming it's because my web browser is being disposed or something with the threads, but it's a global variable! I should be able to reuse it! So I just want to access a couple of webpages and retain state, and I need to use the WebBrowser class.
//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();
}