我想在 webBrowser 是 windows 窗体控件的地方同步调用 webBrowser.Navigate(string urlString)。我这样做

...
private delegate void NavigateDelegate(string s);
...
private void Function()
{
    NavigateDelegate navigateDelegate =
        new NavigateDelegate(this.webBrowser1.Navigate);
    IAsyncResult asyncResult =
        navigateDelegate.BeginInvoke("http://google.com", null, null);

    while (!asyncResult.IsCompleted)
    {
        Thread.Sleep(10);
    }

    MessageBox.Show("Operation has completed !");
}

但消息永远不会被推倒。为什么这段代码不能正常工作?

最佳答案

而是使用它来同步检索页面:

this.webBrowser.DocumentCompleted += WebBrowserDocumentCompleted;
this.webBrowser.Navigate("http://google.com");

private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  MessageBox.Show("Operation has completed !");
}

关于c# - webBrowser.Navigate 同步,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6162381/

10-09 20:25