VB.NET 4.0:希望执行多个线程,但要等到所有线程完成后再恢复
我刚刚在最后一刻有了一个新的想法来承担一项任务,所以我跑到 StackExchange 寻求快速帮助。
我想做的是连续执行一系列方法,每个方法都在自己的线程中。我希望应用程序等待所有这些线程完成,之后程序将恢复。它还必须使用托管线程(线程池)。
您能提供哪些简单的例子来帮助我?如果它太复杂,我应该了解哪些知识,以便我可以自己用 Google 搜索它?
I just had a new, last-minute idea on to take on a task, so I am running to StackExchange for quick help.
What I want to do is execute a series of methods right in a row, each in their own threads. I want the application to wait until all of these threads are completed, after which the program will resume. It also has to use managed threading (thread pool).
What quick examples could you provide to help me along the way? If it's too complex, what things should I know about so that I can Google it on my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 .NET 4,最好使用任务并行库。
在这种情况下,最简单的方法听起来像
Parallel.Invoke
它将使用适当程度的并行性调用每个Action
委托集合,并等待它们全部完成后再返回。如果您需要比这更细粒度的控制,可以将每个作为单独的
任务
并使用Task.WaitAll
等待一切完成。If you're using .NET 4, it would be best to use the Task Parallel Library.
The simplest approach in this case sounds like
Parallel.Invoke
which will invoke each of a collection ofAction
delegates using an appropriate degree of parallelism, and waiting until they've all completed before returning.If you need more fine-grained control than that, you can start each as a separate
Task
and useTask.WaitAll
to wait for everything to finish.