【问题标题】:Does .NET webbrowser.stop() function fire DocumentCompleted event?.NET webbrowser.stop() 函数会触发 DocumentCompleted 事件吗?
【发布时间】:2012-04-03 18:31:23
【问题描述】:

我使用.NET的webbrowser控件打开一堆url,在DocumentCompleted事件中调用循环。

现在我想控制超时。所以我使用了一个计时器,当超时时,它将使用 stop() 函数停止网络浏览器。

问题是:似乎停止函数会触发 DocumentCompleted 事件有时。因此,如果计时器在停止网络浏览器后调用下一个循环,则会发生错误。如果它没有调用下一个循环,有时循环会在中间停止。

这样的程序(不相关的代码被删除):

private string[] urls;//urls are stored here
private int index = 0;//next url index
private void loopFunc()
{
    timer.Enabled = true;
    wb.navigate(urls[index]);
    index++;
    return;
}

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    loopFunc();
}

private void timer1_Tick(object sender, EventArgs e)
{
    wb.stop();
    //loopFunc() or not?
}

我不确定它是否会触发该事件,我通过谷歌一无所获。

【问题讨论】:

  • 我记得 DocumentCompleted 有一个非常奇怪的行为。这帮助我解决了我的问题stackoverflow.com/questions/2328835/…
  • @plurby:多线程是我的建议。但是,也有一些奇怪的行为。
  • 仅供参考,WebBrowser 控件与 C# 无关。它是 .NET 的一部分,而不是 C# 的一部分。

标签: c# browser webbrowser-control


【解决方案1】:

如果我清楚地了解您的情况,这可以解决您的问题:

Timer On --->  loopFunc() --> goto url --> oncomplete -> start timer again -->
                  |
                  |---> Stop timer so it doesn't call loopFunc again

所以在loopFunc之后停止计时器,当下载完成后,再次调用loopFunc

private void loopFunc()
{
    timer.Enabled = true;
    wb.navigate(urls[index]);
    index++;
    timer.stop(); //<<<<<
    return;
}

你的勾号应该是:

private void timer1_Tick(object sender, EventArgs e)
{
  loopFunc();
}

然后在文档完成时再次启动计时器:

private void wb_DocumentCompleted(object sender, 
                  WebBrowserDocumentCompletedEventArgs e)
{
 wb.Stop();
 timer.Start();
}

【讨论】:

  • 使用这种方式可能会出现“资源正在使用中”的异常。 wb.stop() 可以避免这个异常,那就是我使用了stop函数。
  • @Rusty 更新了答案。所以也许可以在documentcompleted 上致电wb.Stop()
  • 如果 wb.stop 触发 DocumentCompleted 事件,不会有错误吗?
  • @Rusty 你确定stop 调用了DocumentCompleted 方法吗?
  • 至少有时。我测试过。
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2011-08-20
  • 2016-10-22
相关资源
最近更新 更多