The best I've been able to find in my research for a "do something with the results of tasks in a list as they complete" is this Microsoft page Process asynchronous tasks as they complete, unfortunately the example within accumulates the results of the tasks which I don't need to do; I only want to: spawn list of tasks, when a task completes, do something with its result.
So here I have adapted their example to demonstrate what I mean:
static async Task SumPageSizesAsync()
{
IEnumerable<Task<int>> downloadTasksQuery =
from url in s_urlList
select ProcessUrlAsync(url, s_client);
List<Task<int>> downloadTasks = downloadTasksQuery.ToList();
while (downloadTasks.Any())
{
Task<int> finishedTask = await Task.WhenAny(downloadTasks);
downloadTasks.Remove(finishedTask);
Console.WriteLine($"Just downloaded ${finishedTask} bytes");
}
}
After any download task finishes, print an alert that it has finished (for example).
How can I more succinctly accomplish the same thing?
Is
downloadTasksQuery.Select(async t => Console.WriteLine($"Just downloaded ${await t} bytes"));
a good solution?