5

I have a list of url and I need to navigate them. How can I make sure that every url will call the DocumentCompleted event ? I've already tried to create many threads and tried using a single thread too but the app is still not firing the event DocumentCompleted for each url.

Is there a way to make a loop in a list of urls and make them call a DocumentCompleted until the thread calls the next url ?

noseratio
  • 59,932
  • 34
  • 208
  • 486
galioni
  • 105
  • 4

2 Answers2

3

To implement this, async/await and Task Parallel Library may come in handy. They allow to have familiar, pseudo-linear code flow for what is an asynchronous logic (handling DocumentCompleted events for multiple navigations, one after another).

I answered a similar question for a WinForm app here and for a console app here.

If you need to target .NET 4.0 but develop with VS2012+ , you still can use async/await, Microsoft provides the Microsoft.Bcl.Async library for that.

If C# 5.0 is not available for this project, you can use yield, as described here.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Sorry, I forgot to say that I´m using .net 4.0, so I can´t use async/await. But thanks for your help, anyway. – galioni Mar 18 '14 at 14:44
  • @galioni, you can use `yield` then, exactly like this: http://stackoverflow.com/a/22296644/1768303. Or, if you need to target .NET 4.0 but develop with VS2012+ , you still can use `async/await`, Microsoft provides the [`Microsoft.Bcl.Async`](http://www.nuget.org/packages/microsoft.bcl.async) library for that. – noseratio Mar 18 '14 at 21:23
  • 1
    It's worked in vs2010 with .net 4.0 ! Thanks a lot! :) – galioni Mar 24 '14 at 14:12
2

If a page has scripts errors on it, it is possible it will never complete. You should make a timeout for that event.

David
  • 853
  • 8
  • 24