Await for list of Tasks
You are looking for Task.WhenAll
:
var tasks = ObjectList
.Where(o => CalculateIfNeedToMakeTaskForO(o))
.Select(o => OTaskAsync(o))
.ToArray();
var results = await Task.WhenAll(tasks);
var combinedResults = results.Select(r => "result for O is: " + r);
You are looking for Task.WaitAll
(assuming your TaskList
implemented IEnumerable<Task>
)
Task.WaitAll(TaskList.ToArray());
Edit: Since WaitAll
only takes an array of task (or a list of Task
in the form of a variable argument array), you have to convert your Enumerable. If you want an extension method, you can do something like this:
public static void WaitAll(this IEnumerable<Task> tasks)
{
Task.WaitAll(tasks.ToArray());
}
TaskList.WaitAll();
But that's really only syntactic sugar.