I have the following code:
List<Task<bool>> tasks = tasksQuery.ToList();
while (tasks.Any())
{
Task<bool> completedTask = await Task.WhenAny(tasks);
if (await completedTask)
return true;
tasks.Remove(completedTask);
}
It launches tasks in parallel. When first completed task returns true the methods returns true.
My question is:
What happens with all remaining tasks that have been launched and probably still running in the background? Is this the right approach to execute a code that is async, parallel and should return after the first condition occurs or it is better to launch them one by one and await singularly?
Thanks
Incidentally, I am just reading Concurrency in C# CookBook, by Stephen Cleary, and I can refer to some parts of the book here, I guess.
From Recipe 2.5 - Discussion, we have
Besides that, I think
WhenAny
is surely the right approach, just consider following Leonid approach of passing the sameCancellationToken
to every task and cancel them after the first one returns. And even though, only in case the cost of these operations are actually taxing the system.