Task.WhenAll
suggest changeIEnumerable<Task<int>> tasks = Enumerable.Range(1, 5).Select(n => Task.Run(() =>
{
Console.WriteLine("I'm task " + n);
return n;
}));
Task<int[]> task = Task.WhenAll(tasks);
int[] results = await task;
Console.WriteLine(string.Join(",", results.Select(n => n.ToString())));
// Output: 1,2,3,4,5
WhenAll preserves the input order. The await must be inside an async method. Task.WhenAll and Task.Run were added in .NET Framework 4.5.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents