问题
I need pause loop until that the webbrowser complete page load.
string[] lines = (string[]) Invoke((ReadLine)delegate
{
return logins.Lines;
});
foreach (string line in lines) {
//..
if (TryParseUserDetails(line, false, out data) {
//...
wb.Navigate(url.Next());
}
}
how to wait the wb page load to before continue loop?
I tried using polling-flags, setting an variable as true in WebBrowserDocumentCompletedEventHandler callback function.
and then:
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(
delegate(object sender2,
WebBrowserDocumentCompletedEventArgs args)
{
done = true;
});
//..
wb.Navigate(url.Next();
while (!done)
{
}
done = false;
I'm looking for something like:
wb.WaitForDone();
Any help is appreciated. Thanks in advance.
回答1:
You can try to use an AutoResetEvent instead of the boolean. Like:
Outside the loop:
AutoResetEvent evt = new AutoResetEvent(false);
Then the event handler:
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(
delegate(object sender2,
WebBrowserDocumentCompletedEventArgs args)
{
evt.Set();
});
and then in the loop:
evt.WaitOne();
回答2:
Why not simply do the stuff you want within the DocumentCompleted Callback like stated here: SO Question?
来源:https://stackoverflow.com/questions/8258062/whats-the-best-way-to-pause-loop-while-webbrowser-is-loading-page