后台工作者+网页浏览器
我尝试使用后台工作者和后台人员浏览几个网页。网络浏览器。我正在使用这个功能,但它不起作用。我不确定这里出了什么问题。
我只看到 MessageBox.Show(arr[0]);没有别的。网络浏览器也没有改变
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();
}
}
}
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
切勿在未检查 RunWorkerCompleted 事件处理程序中的 e.Error 属性的情况下实现 BGW:
有些神秘的无效转换异常是由在工作线程上使用 ReadyState 属性引起的。 WebBrowser 不是线程安全的。检查此答案了解创建WB的方法在工作线程上。
然而,如果您需要保持浏览器对用户可见,那么这不是一个好方法。在这种情况下,您将不得不放弃使用线程。不是真正的问题,只需计算 DocumentCompleted 事件处理程序中的数组索引即可。尽管闪现这些网页没有什么意义。
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.
您无法从 WebBrowser 控件的 UI 线程外部访问其属性。请记住,属性实际上是一种方法,因此您必须使用 Invoke 来调用 Navigate 的原因相同。
Application.DoEvents() 不一定(而且我不确定是否会起作用),因为后台工作线程运行在与 UI 线程不同的线程上。
另外,不要轮询 WebBrowser 的状态,而是使用 DocumentCompleted 事件来获取异步通知:
You can't access the WebBrowser control's property from outside it's UI thread. Remember a property is a method under the covers, so it is the same reason that you must use Invoke to call Navigate.
The Application.DoEvents() isn't necessarily (and I'm not sure will even work) since the background worker is running on a different thread than your UI thread.
Also rather than polling the WebBrowser's state, use the DocumentCompleted event to get your asynchronous notification:
确保AllowNavigation 属性设置为True。
请参阅http://dotnetpulse.blogspot.com/2006/07/为什么-不会-webbrowser-navigate.html
Ensure that AllowNavigation property i set to True.
See http://dotnetpulse.blogspot.com/2006/07/why-wont-webbrowser-navigate.html