.NET 4 equivalent of Task.WhenAll()

白昼怎懂夜的黑 提交于 2019-12-03 05:09:59

I think the closest thing built-in in .Net 4.0 is ContinueWhenAll(). You can make the continuationAction a simple tasks => tasks and use the returned Task.

For performance reasons, you might want to use it with TaskContinuationOptions.ExecuteSynchronously.

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.

The WhenAll and WhenAny methods are then offered on the TaskEx type.

Try waiting on Task.WaitAll() in another Task. Use the LINQ extension method ToArray to convert from IEnumerable<Task> to Task[].

Task WhenAll(IEnumerable<Task> tasks)
{
    return Task.Factory.StartNew(() => Task.WaitAll(tasks.ToArray()));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!