1

What's the best way to code in a wait for the browser to finish process before continuing? I tried this code below and the process is not waiting till its complete before processing the next link.

    void WaitBrowserLoading()
    {
        while (webBrowser1.IsBusy)
            Application.DoEvents();
        for (int i = 0; i < 500; i++)
            if (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
                lblStoreID.Text = "";
                //System.Threading.Thread.Sleep(5);
            }
            else
                break;
        Application.DoEvents();
    }
svick
  • 236,525
  • 50
  • 385
  • 514
acctman
  • 4,229
  • 30
  • 98
  • 142
  • How, as a user, do you know that the webbrowser has finished? – i_am_jorf Sep 03 '11 at 20:06
  • @jeffamphone I have a browser window on the form. I just need a way to halt all coding until WebBrowser1 is finished processing. Have you done this before or are you just inquiring? In VB it's simple but C# doesn't seem to have a solid way of 100% knowing its done. – acctman Sep 03 '11 at 20:11
  • @acctman: What would you do in VB? – Jon Skeet Sep 03 '11 at 20:18
  • 1
    No, I'm conducting a thought exercise. I propose it is impossible to ever know when a webpage has finished. How do you know when a program is finished? How do you know if there is some JS that will wake up in some interval and decide to do more work, to load more data via XHR, etc? – i_am_jorf Sep 03 '11 at 20:58

1 Answers1

0

Do the Navigated or DocumentCompleted events do what you want? I suspect the latter is more appropriate for you:

Handle the DocumentCompleted event to receive notification when the new document finishes loading. When the DocumentCompleted event occurs, the new document is fully loaded, which means you can access its contents through the Document, DocumentText, or DocumentStream property.

Basically you'd disable all the controls (or whatever you want to "stop") before navigating, then re-enable them when the event fires.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194