后台工作者+网页浏览器

发布于 2024-10-09 23:00:24 字数 659 浏览 0 评论 0原文

我尝试使用后台工作者和后台人员浏览几个网页。网络浏览器。我正在使用这个功能,但它不起作用。我不确定这里出了什么问题。

我只看到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半山落雨半山空 2024-10-16 23:00:24

切勿在未检查 RunWorkerCompleted 事件处理程序中的 e.Error 属性的情况下实现 BGW:

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        if (e.Error != null) MessageBox.Show(e.Error.ToString());
    }

有些神秘的无效转换异常是由在工作线程上使用 ReadyState 属性引起的。 WebBrowser 不是线程安全的。检查此答案了解创建WB的方法在工作线程上。

然而,如果您需要保持浏览器对用户可见,那么这不是一个好方法。在这种情况下,您将不得不放弃使用线程。不是真正的问题,只需计算 DocumentCompleted 事件处理程序中的数组索引即可。尽管闪现这些网页没有什么意义。

Never implement BGW without checking the e.Error property in the RunWorkerCompleted event handler:

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        if (e.Error != null) MessageBox.Show(e.Error.ToString());
    }

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.

℡Ms空城旧梦 2024-10-16 23:00:24

您无法从 WebBrowser 控件的 UI 线程外部访问其属性。请记住,属性实际上是一种方法,因此您必须使用 Invoke 来调用 Navigate 的原因相同。

Application.DoEvents() 不一定(而且我不确定是否会起作用),因为后台工作线程运行在与 UI 线程不同的线程上。

另外,不要轮询 WebBrowser 的状态,而是使用 DocumentCompleted 事件来获取异步通知:

 int i = 0;
 string[] arr = { "http://stackoverflow.com/", "http://www.codeproject.com/", "http://www.codeplex.com/" };

 protected override void OnShown(EventArgs e)
 {
     base.OnShown(e);
     bB_checker.DocumentCompleted += bB_checker_DocumentCompleted;
     bB_checker.Navigate(arr[0]);
 }

 void bB_checker_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
     i++;
     if (i < arr.Length)
        bB_checker.Navigate(arr[i]);
 }

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:

 int i = 0;
 string[] arr = { "http://stackoverflow.com/", "http://www.codeproject.com/", "http://www.codeplex.com/" };

 protected override void OnShown(EventArgs e)
 {
     base.OnShown(e);
     bB_checker.DocumentCompleted += bB_checker_DocumentCompleted;
     bB_checker.Navigate(arr[0]);
 }

 void bB_checker_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
     i++;
     if (i < arr.Length)
        bB_checker.Navigate(arr[i]);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文