0

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!

CJH
  • 1,266
  • 2
  • 27
  • 61
  • Could you provide some code to show what you have tried so far? Is it a WPF or a WinForm application? Also some diagnostic traces would be useful, if available – Evil Toad Jul 14 '15 at 13:59
  • I have just added some code snippets to the original question in hope that it may help in getting to the solution to my problem. – CJH Jul 14 '15 at 14:17

1 Answers1

0

I used:

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

after the Navigate function and it now works as expected.

CJH
  • 1,266
  • 2
  • 27
  • 61
  • This is bad: 1) it uses a [busy waiting loop](https://en.wikipedia.org/wiki/Busy_waiting) burning your CPU cores in vain; 2) It uses `DoEvents` which is [almost always an evil](http://blogs.msdn.com/b/jfoscoding/archive/2005/08/06/448560.aspx). – noseratio Jul 14 '15 at 23:17
  • Thanks Noseratio - Do you have any suggestions on how this can be achieved any other way? – CJH Jul 15 '15 at 07:25
  • I do, e.g. [this](http://stackoverflow.com/a/22262976/1768303), I think I posted it before; or [this](http://stackoverflow.com/a/19718530/1768303). – noseratio Jul 15 '15 at 07:44
  • Those both look great! I am looking particularly at the one using the example.com test site - but am unable to get it working in a winform...?? Is this specifically written to work as a console application? – CJH Jul 15 '15 at 15:16
  • [This one](http://stackoverflow.com/a/20934538/1768303) does it as a WinForms app. – noseratio Jul 15 '15 at 20:45
  • 1
    Bingo!! Exactly what I was after! Thank you so much for that! Exactly what I was looking for and have been able to use this framework to achieve what I was after! – CJH Jul 16 '15 at 08:01