C#TPL如何知道所有的任务都做了什么? [英] C# TPL how to know that all tasks are done?

查看:266
本文介绍了C#TPL如何知道所有的任务都做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成任务的循环

I have the Loop which generates tasks.

代码:

Task task = null;
foreach (Entity a in AAAA)
{
  // create the task 
  task = new Task(() => {
    myMethod(a);
  },  Token, TaskCreationOptions.None);
  task.Start();
}

你可以在每次迭代任务对象看到了新的初始化(..new任务(()=> ..)
我怎么能知道所有的任务呢?

As you can see in each iteration task object has new initialization (..new Task(() =>..) How can I know that all tasks done?

推荐答案

我F你以

 Parallel.ForEach(...,  () => myMethod(a), ...)

然后你得到的所有任务自动伺候的foreach结束。

Then you get an automatic Wait on all tasks at the end of the ForEach.

和可能运行一个单独的任务给ForEach。

And maybe run the ForEach from a separate Task.

这篇关于C#TPL如何知道所有的任务都做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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