运行多个异步任务,并等待他们全部完成 [英] Running multiple async tasks and waiting for them all to complete

查看:328
本文介绍了运行多个异步任务,并等待他们全部完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在控制台应用程序运行多个异步任务,并等待他们都在进一步处理之前完成。

I need to run multiple async tasks in a console application, and wait for them all to complete before further processing.

有许多文章在那里,但我似乎得到更糊涂的我越读。我已阅读并理解任务库的基本原则,但我显然缺少一个环节的某个地方。

There's many articles out there, but I seem to get more confused the more I read. I've read and understand the basic principles of the Task library, but I'm clearly missing a link somewhere.

据我所知,这是可能的连锁任务,让他们又完成之后启动(这是pretty的大部分场景都是我读过的文章),但我想我所有的任务运行的同时,我想知道,一旦他们都完成了。

I understand that it's possible to chain tasks so that they start after another completes (which is pretty much the scenario for all the articles I've read), but I want all my Tasks running at the same time, and I want to know once they're all completed.

什么是最简单的实现了情景是这样?

What's the simplest implementation for a scenario like this?

推荐答案

您可以创建许多任务,如:

you could create many tasks like:

List<Task> TaskList = new List<Task>();
foreach(...)
{
    var LastTask = new Task(SomeFunction);
    LastTask.Start();
    TaskList.Add(LastTask);
}

Task.WaitAll(TaskList.ToArray());

希望它可以帮助...

Hope it helps...

这篇关于运行多个异步任务,并等待他们全部完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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