Questions tagged «cancellationtokensource»

5
为什么CancellationToken与CancellationTokenSource分开?
我正在寻找为什么CancellationToken除了CancellationTokenSource类之外还引入了.NET 结构的理由。我了解如何使用API​​,但也想了解为什么采用这种方式设计。 即,为什么有: var cts = new CancellationTokenSource(); SomeCancellableOperation(cts.Token); ... public void SomeCancellableOperation(CancellationToken token) { ... token.ThrowIfCancellationRequested(); ... } 而不是CancellationTokenSource像这样直接传递: var cts = new CancellationTokenSource(); SomeCancellableOperation(cts); ... public void SomeCancellableOperation(CancellationTokenSource cts) { ... cts.ThrowIfCancellationRequested(); ... } 这是否基于以下事实进行性能优化:取消状态检查比传递令牌更频繁? 这样就CancellationTokenSource可以跟踪和更新CancellationTokens,对于每个令牌,取消检查是本地访问? 鉴于在两种情况下没有锁定的挥发性布尔变量就足够了,我仍然不明白为什么这样做会更快。 谢谢!
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.