简体   繁体   中英

.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?

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();
    }
}

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 .

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