[英]Can I use CancellationTokenSource.Cancel and CancellationTokenSource.CancelAfter(timeSpan) for the same task?
I didn't see this in the docs anywhere -我在任何地方的文档中都没有看到这个 -
If I want to use CancellationTokenSource.CancelAfter(600000)
as a failsafe in case my CancellationTokenSource.Cancel()
call never gets called.如果我想使用
CancellationTokenSource.CancelAfter(600000)
作为故障保护,以防我的CancellationTokenSource.Cancel()
调用永远不会被调用。
Will the CancelAfter()
interfere with the Cancel()
call in any way? CancelAfter()
会以任何方式干扰Cancel()
调用吗?
Nope, no interference.不,没有干扰。 When a
CancellationTokenSource
is canceled, it's an atomic operation. CancellationTokenSource
时,它是一个原子操作。 Either the Cancel
will occur first and will dispose immediately the active CancelAfter
-related timer, or the timer will be triggered first and the subsequent Cancel
will be a no-op. Cancel
将首先发生并立即处理活动的CancelAfter
相关计时器,或者计时器将首先被触发,随后的Cancel
将是空操作。
If you want you can study the source code of the Cancel
method here .如果需要,可以在此处研究
Cancel
方法的源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.