Making the webbrowser control synchronous

Posted by andy gaskell on Apr 1st, 2006

Here’s a little trick if you need to make the WebBrowser control behave synchronously instead of having to hookup to the DocumentCompleted event. Attaching/unattaching from that event makes code pretty messy. Anyway, here’s my solution – it works :)

this.webBrowser.Navigate(url);

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

So simpo.

Technorati Tags: ,

7 Responses

  1. dlyle Says:

    There is some danger in using Application.DoEvents() in that if you open and close your application quickly, you might get rogue process instances if you view your task manager — especially with the Application.DoEvents() inside a while loop.

  2. Josh Says:

    thanks so much for that tip, very simple, yet very effective. Does exactly what I need too.

    You saved me a TON of time.

  3. Naveed Qadri Says:

    Thanks man !!!
    You are a life savor.

    NBQ -

  4. Alan Says:

    Thanks, this saved a lot of my time.

  5. Marl Atkins Says:

    That solution will leave your CPU humming and eating up resources like mad.

    I add a System.Threading.Thread.Sleep(50); to help but even that doesn’t help much.

    I really wish I could come up with a good solution for this.
    I NEED to be able to make it synchronous.

  6. Dale Wilson Says:

    The thread.sleep does nothing but delay the entire thread – essentially holding everything up.

    I am happy with the DoEvents solution.

  7. Vijay Says:

    But what happen when ready state in readyState_InteractiveClick

    It is happen when filedownload box display and click on cancel button then ready state is readySate_InteractiveClick

    How to get back in readyState_Completed

    Please Help Me
    Thanks In advaced
    VJ

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.