I have tried to navigate through a couple of web pages using an backgroundworker & webbrowser. Im using this function which it doesnt work. Im not sure what is going wrong here.
I see only the MessageBox.Show(arr[0]); nothing else. the webbrowser doesnt change too
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
string[] arr = { "http://stackoverflow.com/", "http://www.codeproject.com/", "http://www.codeplex.com/" };
for (int i = 0; i < 3; i++)
{
MessageBox.Show(arr[i]);
bB_checker.Invoke((EventHandler)delegate { bB_checker.Navigate(arr[i]); });
while (bB_checker.ReadyState != WebBrowserReadyState.Complete)
{
// System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
}
}
Never implement BGW without checking the e.Error property in the RunWorkerCompleted event handler:
The somewhat mysterious invalid cast exception is caused by using the ReadyState property on a worker thread. WebBrowser is not threadsafe. Check this answer for a way to create a WB on a worker thread.
That’s not however a good way if you need to keep the browser visible to the user. You’ll have to give up on using threading in that case. Not a real problem, just count up the array index in the DocumentCompleted event handler. Albeit that it makes little sense to flash these web pages.