问题描述
我从此处了解到Task.Whenall 不是确定性的,但我找不到有关结果顺序的任何信息.
I understand from here that the task execution order for
Task.Whenall
is not deterministic but I cannot find any information about result order.
结果集合是否会按照输入中的任务排序或结果可以按任何顺序包含结果?
Will the results collection contain the results in the order in which the tasks where ordered in the input or the results can be in any order?
从我做的测试来看,它似乎保留了订单,但我需要确认.
From the tests that I did, it seems to keep the order but I need a confirmation.
推荐答案
来自 MSDN:
Task.WhenAll<TResult>(IEnumerable<Task<TResult>>)
这是包含此语句的四个重载中唯一的一个:
This is the only overload of the four which contains this statement:
如果没有任务发生故障并且没有任务被取消,则结果任务将以 RanToCompletion 状态结束.的结果返回的任务将被设置为一个包含所有所提供任务的结果按照提供的顺序(例如,如果输入任务数组包含 t1、t2、t3,则输出任务的结果将返回一个 TResult[],其中 arr[0] == t1.Result, arr1 ==t2.Result, 和 arr[2] == t3.Result).
这篇关于Task.WhenAll 结果排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!