问题描述
我有一个URL列表,我需要浏览它们.如何确保每个URL都将调用DocumentCompleted事件?我已经尝试创建多个线程,也尝试使用单个线程,但是该应用程序仍未触发每个URL的事件DocumentCompleted.
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.
有没有一种方法可以在URL列表中循环并使它们调用DocumentCompleted,直到线程调用下一个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 ?
推荐答案
要实现此目的, async/await
和任务并行库可能会派上用场.它们允许使用熟悉的伪线性代码流来表示异步逻辑(处理多次导航的DocumentCompleted
事件,一个接一个).
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.
如果您需要定位.NET 4.0但使用VS2012 +进行开发,则仍然可以使用async/await
,Microsoft提供了 Microsoft.Bcl.Async
库.
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.
如果该项目没有C#5.0,则可以使用yield
,如此处所述..
If C# 5.0 is not available for this project, you can use yield
, as described here.
这篇关于如何使用网络浏览器浏览链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!