任务构造函数中的取消标记:为什么? [英] Cancellation token in Task constructor: why?

查看:27
本文介绍了任务构造函数中的取消标记:为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些 System.Threading.Tasks.Task 构造函数将 CancellationToken 作为参数:

Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter:

CancellationTokenSource source = new CancellationTokenSource();
Task t = new Task (/* method */, source.Token);

让我感到困惑的是,没有办法从内部方法体中实际获取传入的令牌(例如,没有像 Task.CurrentTask.CancellationToken).令牌必须通过某种其他机制提供,例如状态对象或在 lambda 中捕获.

What baffles me about this is that there is no way from inside the method body to actually get at the token passed in (e.g., nothing like Task.CurrentTask.CancellationToken). The token has to be provided through some other mechanism, such as the state object or captured in a lambda.

那么在构造函数中提供取消令牌的目的是什么?

So what purpose does providing the cancellation token in the constructor serve?

推荐答案

CancellationToken 传递到 Task 构造函数中,将其与任务相关联.

Passing a CancellationToken into the Task constructor associates it with the task.

引用 Stephen Toub 的回答来自 MSDN:

这有两个主要好处:

  1. 如果令牌在 Task 开始执行之前请求取消,Task 将不会执行.而不是过渡到Running,它会立即过渡到Canceled.这避免了运行任务的成本,如果它只是在运行时被取消无论如何.
  2. 如果任务主体也在监视取消标记并抛出包含该标记的 OperationCanceledException(这就是 ThrowIfCancellationRequested 所做的),然后当任务看到OperationCanceledException,它检查OperationCanceledException的token是否与Task的token匹配令牌.如果是,则该例外被视为对合作取消和 Task 转换到 Canceled状态(而不是 Faulted 状态).
  1. If the token has cancellation requested prior to the Task starting to execute, the Task won't execute. Rather than transitioning to Running, it'll immediately transition to Canceled. This avoids the costs of running the task if it would just be canceled while running anyway.
  2. If the body of the task is also monitoring the cancellation token and throws an OperationCanceledException containing that token (which is what ThrowIfCancellationRequested does), then when the task sees that OperationCanceledException, it checks whether the OperationCanceledException's token matches the Task's token. If it does, that exception is viewed as an acknowledgement of cooperative cancellation and the Task transitions to the Canceled state (rather than the Faulted state).

这篇关于任务构造函数中的取消标记:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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