.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 t开发者_如何学运维he 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
.
精彩评论