Task.WhenAny-其余正在运行的任务会怎样? [英] Task.WhenAny - What happens with remaining running tasks?

查看:152
本文介绍了Task.WhenAny-其余正在运行的任务会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

List<Task<bool>> tasks = tasksQuery.ToList();
while (tasks.Any())
{
    Task<bool> completedTask = await Task.WhenAny(tasks);
    if (await completedTask)
        return true;

    tasks.Remove(completedTask);
}

它并行启动任务.当第一个完成的任务返回true时,方法将返回true.

我的问题是:

已启动并可能仍在后台运行的所有其余任务会发生什么情况? 这是执行异步,并行并在第一个条件发生后应返回的代码的正确方法吗?还是最好逐个启动它们并等待一个?

谢谢

解决方案

顺便说一句,我只是在阅读 C#CookBook中的并发性,由 Stephen Cleary 进行,我可以我猜这里是本书的某些部分.

我们从第2.5版-讨论中获得了

第一个任务完成时,请考虑是否取消其余任务.如果其他任务没有取消但也从未等待,则将其放弃. 被遗弃的任务将运行完毕,其结果将被忽略.那些被遗弃的任务中的任何异常也将被忽略.

Task的另一种反模式.WhenAny在完成任务时对其进行处理. 首先,似乎很合理的方法是保留任务列表并在完成时从列表中删除每个任务.这种方法的问题在于,当存在O(N)算法时,它会在O(N ^ 2)时间执行.

除此之外,我认为WhenAny肯定是正确的方法,只需考虑遵循 Leonid 传递方法对每个任务都使用相同的CancellationToken,并在第一个任务返回后将其取消.即使如此,只有在这些操作的成本实际上使系统负担沉重的情况下.

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

When the first task completes, consider whether to cancel the remaining tasks. If the other tasks are not canceled but are also never awaited, then they are abandoned. Abandoned tasks will run to completion, and their results will be ignored. Any exceptions from those abandoned tasks will also be ignored.

Another antipattern for Task.WhenAny is handling tasks as they complete. At first it seems like a reasonable approach to keep a list of tasks and remove each task from the list as it completes. The problem with this approach is that it executes in O(N^2) time, when an O(N) algorithm exists.

Besides that, I think WhenAny is surely the right approach, just consider following Leonid approach of passing the same CancellationToken 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.

这篇关于Task.WhenAny-其余正在运行的任务会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆