Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webBrowser.Navigate synchronously

Tags:

browser

c#

.net

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?

like image 474
Disappointed Avatar asked Jan 19 '23 15:01

Disappointed


2 Answers

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

while (this.webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
     Application.DoEvents();
     Thread.Sleep(100);
}
like image 169
VikciaR Avatar answered Jan 28 '23 19:01

VikciaR


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 !");
}
like image 44
Teoman Soygul Avatar answered Jan 28 '23 19:01

Teoman Soygul