I try to find a solution of this but I didn't able to find it. That's why I'm asking here. :)
I'm having a list of tasks and It has two items
List<Task<int>> tasks = new List<Task<int>>();
tasks.Add(Task.FromResult(1));
tasks.Add(Task.FromResult(2));
When I call await Task.WhenAll(tasks), it returns int[] but I want it should return List<int>
. Like below:
List<int> result = await Task.WhenAll(tasks);
I want to add new item in the list after I've the result.
WhenAll can run a bunch of async methods in parallel and returns when every one finished.
WhenAll returns control after all tasks are completed, while WhenAny returns control as soon as a single task is completed.
WhenAll reifies your task sequence so it knows how many tasks it needs to wait for.
WhenAll starts both Tasks at the same time and executes them in parallel, the outcome is that instead of taking 3 seconds to run the program it just takes 2 seconds, that's a huge performance enhancement!
You can write:
List<int> result = (await Task.WhenAll(tasks)).ToList();
This will convert the array to a list.
Do not forget to add the namespace:
using System.Linq;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With