what's the best way to pause loop while webBrowser is loading page?

折月煮酒 提交于 2019-12-25 08:47:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!