Firing WebBrowser.DocumentCompleted event whilst in a loop

你说的曾经没有我的故事 提交于 2019-12-10 11:48:17

问题


I have a simple app I am developing that needs to iterate through a list of URLs which are passed to a WebBrowsers Navigate function in a for each loop. I was hoping to see the DocumentCompleted event firing after each call of the Navigate function but it only seems to be fired after the whole form has completed loading - and this the loop has completed.

I guess I am missing something fundamental here but some help and advice would be great!

Thanks!

Here is a sample of code that I am trying...

This foreach loop runs n the Form Load event of the WinForms page I am using...

            int id = 0;
        foreach (DataRow row in quals.Rows)
        {
            URN = row["LAIM_REF"].ToString();

            string URN_formated = URN.Replace("/", "_");
            string URL = "http://URL_I_AM_GOING_TOO/";
            string FullURL = URL + URN_formated;

            wbrBrowser.ScriptErrorsSuppressed = true;
            wbrBrowser.Refresh();
            wbrBrowser.Navigate(FullURL);

            id += 1;

            label1.Text = id.ToString();

        }

At the point the loop gets to the line:

wbrBrowser.Navigate(FullURL);

I was hoping that the event:

        private void wbrBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
...
}

would fire therefore being able to run processes against each of the URLs returned in the loop.

Thanks!


回答1:


I used:

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

after the Navigate function and it now works as expected.



来源:https://stackoverflow.com/questions/31408266/firing-webbrowser-documentcompleted-event-whilst-in-a-loop

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