• 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.

           

  • 相关阅读:
    学习 | mockjs入门
    学习 | 基于require.js的三级联动菜单【入门】
    Android_百度地图基本用法5
    Android_百度地图基本用法4
    Android_百度地图基本用法3
    Android_百度地图基本用法2
    Android_百度地图基本用法1
    Android_Event Bus 的基本用法
    Kotlin基础-扩展
    Kotlin基础-泛型
  • 原文地址:https://www.cnblogs.com/chucklu/p/12877198.html
Copyright © 2020-2023  润新知