I have a list of tasks that I created like this:
public async Task<IList<Foo>> GetFoosAndDoSomethingAsync() { var foos = await GetFoosAsync(); var tasks = foos.Select(async foo => await DoSomethingAsync(foo)).ToList(); ... }
By using .ToList()
, the tasks should all start. Now I want to await their completion and return the results.
This works in the above ...
block:
var list = new List<Foo>(); foreach (var task in tasks) list.Add(await task); return list;
It does what I want, but this seems rather clumsy. I'd much rather write something simpler like this:
return tasks.Select(async task => await task).ToList();
... but this doesn't compile. What am I missing? Or is it just not possible to express things this way?
LINQ doesn't work perfectly with async code, but you can do this: var tasks = foos.Select (DoSomethingAsync).ToList (); await Task.WhenAll (tasks); If your tasks all return the same type of value, then you can even do this: which is quite nice. WhenAll returns an array, so I believe your method can return the results directly:
Each call to ProcessUrlAsync in the following code returns a Task<TResult>, where TResult is an integer: Due to deferred execution with the LINQ, you call Enumerable.ToList to start each task. The while loop performs the following steps for each task in the collection:
Due to deferred execution with the LINQ, you call Enumerable.ToList to start each task. The while loop performs the following steps for each task in the collection: Awaits a call to WhenAny to identify the first task in the collection that has finished its download.
It means that the query is blocking. So it is not really asynchronous. will first start an asynchronous operation for each event. Then this line: will wait for those operations to complete one at a time (first it waits for the first event's operation, then the next, then the next, etc).
LINQ doesn't work perfectly with async
code, but you can do this:
var tasks = foos.Select(DoSomethingAsync).ToList(); await Task.WhenAll(tasks);
If your tasks all return the same type of value, then you can even do this:
var results = await Task.WhenAll(tasks);
which is quite nice. WhenAll
returns an array, so I believe your method can return the results directly:
return await Task.WhenAll(tasks);
To expand on Stephen's answer, I've created the following extension method to keep the fluent style of LINQ. You can then do
await someTasks.WhenAll() namespace System.Linq { public static class IEnumerableExtensions { public static Task<T[]> WhenAll<T>(this IEnumerable<Task<T>> source) { return Task.WhenAll(source); } } }
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