Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.WhenAll result ordering

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.

like image 842
user3552661 Avatar asked Apr 19 '14 21:04

user3552661


People also ask

What does WhenAll return?

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. The call to WhenAll(Task[]) method does not block the calling thread.

How does task WhenAll work?

Task. WhenAll creates a task that will complete when all of the supplied tasks have been completed. It's pretty straightforward what this method does, it simply receives a list of Tasks and returns a Task when all of the received Tasks completes.

Can you tell difference between task WhenAll and task WhenAny?

WhenAll returns control after all tasks are completed, while WhenAny returns control as soon as a single task is completed.

Does task WhenAll run in parallel?

WhenAll() method in . NET Core. This will upload the first file, then the next file. There is no parallelism here, as the “async Task” does not automatically make something run in in parallel.


1 Answers

From MSDN:

Task.WhenAll<TResult>(IEnumerable<Task<TResult>>)

This is the only overload of the four which contains this statement:

If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the RanToCompletion state. The Result of the returned task will be set to an array containing all of the results of the supplied tasks in the same order as they were provided (e.g. if the input tasks array contained t1, t2, t3, the output task's Result will return an TResult[] where arr[0] == t1.Result, arr1 == t2.Result, and arr[2] == t3.Result).

like image 55
Yuval Itzchakov Avatar answered Oct 18 '22 02:10

Yuval Itzchakov