【问题标题】:WebBrowser run on same threadWebBrowser 在同一线程上运行
【发布时间】:2014-06-10 10:34:48
【问题描述】:

我需要防止 WebBrowser.Navigate() 在同一个线程上运行,因为我需要进程有一定的顺序。

我遇到了Async/Await implementation of WebBrowser class for .NET,它使用 async/await 实现,但我必须使用不支持它的 .net 3.5。

有人知道其他方法吗?

【问题讨论】:

  • 不能阻止它在同一个线程上运行,WebBrowser 是一个单线程的 COM 对象。 async/await 的简单替代方案是状态机。或者在单独的线程上运行所有内容,如shown here
  • 如果不能使用async/await,或许可以使用IEnumerable/yield:stackoverflow.com/a/22296644/1768303

标签: c# webbrowser-control


【解决方案1】:

据我了解您的要求,您可以通过以下方式使用WebBrowser 组件的DocumentCompleted 事件:

webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Loaded);
webBrowser.Navigate(...);

...

void Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // do stuff that needs to be run after the page is loaded
}

【讨论】:

    【解决方案2】:

    我最终在主窗体上设置了一个标志。然后有一个thread.sleep,直到flag被重置。

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多