webBrowser.Navigate synchronously

孤街醉人 提交于 2019-12-31 00:01:35

问题


i want to call webBrowser.Navigate(string urlString) synchronously where webBrowser is windows forms control. I do this in such way

...
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 !");
}

but message is never shoved. WHY this code doesn't work properly?


回答1:


Rather use this to retrieve the page syncronously:

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

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



回答2:


Not the best way, but you can use this...

while (this.webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
     Application.DoEvents();
     Thread.Sleep(100);
}


来源:https://stackoverflow.com/questions/6162381/webbrowser-navigate-synchronously

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!