Get the result of Task.WhenAll

I have several tasks returning the same type of object that I want to call using Task.WhenAll(new[]{t1,t2,t3}); and reading the results.

When i try to use

 Task<List<string>> all = await Task.WhenAll(new Task[] { t, t2 }).ConfigureAwait(false); 

I get a compiler error

It is not possible to implicitly convert the type 'void' to 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>

both tasks invoke a method similar to this.

 private Task<List<string>> GetFiles(string path) { files = new List<string>(); return Task.Run(() => { //remove for brevity return files; }); } 
+6
source share
2 answers

It looks like you are using WaitAll() overload, which does not return a value. If you make the following changes, it should work.

 List<string>[] all = await Task.WhenAll(new Task<List<string>>[] { t, t2 }) .ConfigureAwait(false); 
+5
source

The return type WhenAll is a task whose result type is an array of the result type of individual tasks, in your case Task<List<string>[]>

When used in an expectation expression, the task will "unfold" into its result type, which means that the type of your "all" variable must be List<string>[]

+2
source

All Articles