简体   繁体   中英

webBrowser.Navigate synchronously

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM