简体   繁体   English

.NET 4相当于Task.WhenAll()

[英].NET 4 equivalent of Task.WhenAll()

In .NET 4, is there any functional equivalent to .NET 4.5's System.Threading.Tasks.Task.WhenAll() ? 在.NET 4中,是否有任何功能等同于.NET 4.5的System.Threading.Tasks.Task.WhenAll()

The goal is to wrap up multiple async tasks into a single one that is completed when all of its constituent tasks are done. 目标是将多个异步任务包装成一个在完成所有组成任务时完成的异步任务。

In .NET Framework 4.0 WhenAll and WhenAny can be used with installed AsyncCTP in case of Visual Studio 2010 or Async Targeting Pack in case of Visual Studio 2012. 在.NET Framework 4.0中,对于Visual Studio 2010或Async Targeting Pack,在Visual Studio 2012的情况下,WhenAll和WhenAny可与已安装的AsyncCTP一起使用。

The WhenAll and WhenAny methods are then offered on the TaskEx type. 然后在TaskEx类型上提供WhenAll和WhenAny方法。

I think the closest thing built-in in .Net 4.0 is ContinueWhenAll() . 我认为.Net 4.0中最接近的东西是ContinueWhenAll() You can make the continuationAction a simple tasks => tasks and use the returned Task . 您可以使continuationAction成为一个简单的tasks => tasks并使用返回的Task

For performance reasons, you might want to use it with TaskContinuationOptions.ExecuteSynchronously . 出于性能原因,您可能希望将它与TaskContinuationOptions.ExecuteSynchronously一起使用。

Try waiting on Task.WaitAll() in another Task. 尝试在另一个任务中等待Task.WaitAll() Use the LINQ extension method ToArray to convert from IEnumerable<Task> to Task[] . 使用LINQ扩展方法ToArrayIEnumerable<Task>转换为Task[]

Task WhenAll(IEnumerable<Task> tasks)
{
    return Task.Factory.StartNew(() => Task.WaitAll(tasks.ToArray()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM