VB.NET 4.0:希望执行多个线程,但是要等到所有线程完成后才能继续 [英] VB.NET 4.0: Looking to execute multiple threads, but wait until all threads are completed before resuming

查看:243
本文介绍了VB.NET 4.0:希望执行多个线程,但是要等到所有线程完成后才能继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚有一个新的,紧要的想法来执行一项任务,所以我正在运行StackExchange以获得快速帮助.

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?

推荐答案

如果您使用的是.NET 4,则最好使用任务并行库.

If you're using .NET 4, it would be best to use the Task Parallel Library.

在这种情况下,最简单的方法听起来像 Parallel.Invoke ,它将使用适当的并行度调用Action委托的每个集合,并等待它们全部完成后再返回.

The simplest approach in this case sounds like Parallel.Invoke which will invoke each of a collection of Action 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 use Task.WaitAll to wait for everything to finish.

这篇关于VB.NET 4.0:希望执行多个线程,但是要等到所有线程完成后才能继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆