JOE SKEET
JOE SKEET

Reputation: 8118

.NET webbrowser - documentcompleted not responding correctly

i am doing this:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (webBrowser1.DocumentText.IndexOf("Negative Orders") != -1)
    {
        webBrowser1.Navigate(@"http://............somepage");

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

        MessageBox.Show("finished loading");
    }
}

something very weird is happening. it is succesfully entering the IF STATEMENT; however once it executes the webBrowser.Navigate, it enter the while, and after the while it simply returns. it does not do the messagebox at all.

what is going on here?

Upvotes: 1

Views: 1278

Answers (2)

Juan
Juan

Reputation: 15715

I believe your message box is not being called because your code is still in the Application.DoEvents(); section deep down your call stack. It is probably waiting for you to leave the webBrowser1_DocumentCompleted method to invoke the next DocumentCompleted handler and finally set the ReadyState to Complete.

I don't recommend using Application.DoEvents(); at all, but specially not inside webBrowser1_DocumentCompleted.

Upvotes: 0

Joshua Evensen
Joshua Evensen

Reputation: 1566

It sounds like an exception is occurring. Try putting the whole thing in a try catch and breaking when an exception occurs

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    try
    {
        if (webBrowser1.DocumentText.IndexOf("Negative Orders") != -1)
        {
            webBrowser1.Navigate(@"http://............somepage");

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

            MessageBox.Show("finished loading");
        }
    }
    catch (Exception x)
    {
        System.Diagnostics.Debugger.Break();
    }
}

Upvotes: 1

Related Questions