开发者

webBrowser.Navigate synchronously

开发者 https://www.devze.com 2023-03-08 19:23 出处:网络
i want to call webBrowser.Navigate(string urlString) synchronously where webBrowser is windows forms control. I do this in such way

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 vo开发者_高级运维id 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?


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

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


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

精彩评论

暂无评论...
验证码 换一张
取 消