WaitAll vs WhenAll
WaitAll vs WhenAll
回答1
Task.WaitAll
blocks the current thread until everything has completed.
Task.WhenAll
returns a task which represents the action of waiting until everything has completed.
That means that from an async method, you can use:
await Task.WhenAll(tasks);
... which means your method will continue when everything's completed, but you won't tie up a thread to just hang around until that time.
回答2
While JonSkeet's answer explains the difference in a typically excellent way there is another difference: exception handling.
Task.WaitAll
throws an AggregateException
when any of the tasks throws and you can examine all thrown exceptions. The await
in await Task.WhenAll
unwraps the AggregateException
and 'returns' only the first exception.
When the program below executes with await Task.WhenAll(taskArray)
the output is as follows.
WaitAll(Task[])
Waits for all of the provided Task objects to complete execution.
WhenAll(Task[])
Creates a task that will complete when all of the Task objects in an array have completed.
public static System.Threading.Tasks.Task WhenAll (params System.Threading.Tasks.Task[] tasks);
Remarks
The overloads of the WhenAll method that return a Task object are typically called when you are interested in the status of a set of tasks or in the exceptions thrown by a set of tasks.
Note
The call to WhenAll(Task[]) method does not block the calling thread.
If any of the supplied tasks completes in a faulted state, the returned task will also complete in a Faulted state, where its exceptions will contain the aggregation of the set of unwrapped exceptions from each of the supplied tasks.
If none of the supplied tasks faulted but at least one of them was canceled, the returned task will end in the Canceled state.
If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the RanToCompletion state.
If the supplied array/enumerable contains no tasks, the returned task will immediately transition to a RanToCompletion state before it's returned to the caller.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-05-12 UML的类型
2017-05-12 IIS Modules Overview
2017-05-12 Introduction to IIS Architectures