0

I have problem with code below. Can some one tell why it doesn't work in BackgroundWorker and how can i solve this problem

  string address = "My URL";
            webBrowser.Navigate(new Uri(address));
            do
            {
                Application.DoEvents();
            } while (webBrowser.ReadyState != WebBrowserReadyState.Complete);
Echilon
  • 10,064
  • 33
  • 131
  • 217
arash Dman
  • 333
  • 5
  • 15

1 Answers1

1

No!!

You better open a new thread and instruct the WebBrowser from there

Application.DoEvents() is kinda evil.

Here is how you can start

System.Threading.Thread t = new System.Threading.Thread(() =>
{
    yourWebBrowser.Navigate("http://Google.com");
});

t.ApartmentState = System.Threading.ApartmentState.STA;
t.Start();

To get notified that the page has been loaded you can subscribe to the DocumentCompleted event as so:

yourWebBrowser.DocumentCompleted += WebBrowserDocumentCompleted;

void WebBrowserDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
    throw new NotImplementedException();
}
Community
  • 1
  • 1
Odys
  • 8,951
  • 10
  • 69
  • 111
  • No because backgroundWorker has an ApartmentState of `MTA` instead of `STA` that webbrowser needs – Odys Nov 27 '12 at 07:42
  • @arash Did this worked out? Consider accepting this answer if it resolves your issue – Odys Nov 27 '12 at 15:22