I need to run multiple awaitable tasks, and then gather their results into a list and return it.
I can create the tasks in a loop and use Task.WhenAll to await them, but I can't figure out how to access the results of each awaited task. I tried the below but Result is not defined.
List<Service> services = new List<Service>();
List<Exception> exceptions = new List<Exception>();
List<Task<Service>> tasks = new List<Task<Service>>();
foreach (string serviceMoniker in monikers)
{
try
{
tasks.Add(GetService(serviceMoniker, tenantMoniker, countryCode, environmentId));
}
catch (Exception e) { exceptions.Add(e); }
}
var continuation = Task.WhenAll(tasks);
for (int i=0; i < continuation.Result.Length - 1; i++)
{
services.Add(continuation.Result[i].Result);
}
another attempt
await Task.WhenAll(tasks);
foreach (Task t in tasks)
{
services.Add(t.Result);
}