`CancellationTokenSource.TryReset` 的用例是什么?

What is the use-case for `CancellationTokenSource.TryReset`?

CancellationTokenSource 有一个 TryReset() 成员。 documentation 看起来如此严格,让我想知道它为什么存在。

那么为什么人们会为 TryReset 烦恼呢?为什么不简单地为每个异步操作创建一个全新的 CancellationTokenSource ,然后在操作完成并且不再需要取消后将其丢弃? CancellationTokenSource 创建一个非常昂贵的对象吗?

让我在这里引用问题的背景和动机部分

When a library or framework exposes a CancellationToken that usually does not get canceled (e.g. HttpContext.RequestAborted) they often still need to dispose the backing CancellationTokenSource (CTS) after an operation completes rather than reusing it in order to account for callers that might never dispose their registrations.

To use the RequestAborted example, Kestrel disposes the backing CTS after every request where the RequestAborted token is accessed. If Kestrel attempted to reuse the CTS backing the RequestAborted token for future requests in order to reduce allocations, it would risk leaking any undisposed registrations and triggering the undisposed registrations when unrelated requests are aborted.

Another scenario where people want to be able to "reset" a CTS is after calling CancelAfter(). This can already be achieved by calling CancelAfter(Timeout.Infinite), but that's not necessarily obvious unless you read the docs. TryReset() is something that would immediately make sense in this scenario when looking at intellisense completions.

Another benefit is that it's immediately obvious if resetting failed. If you try resetting a timeout with CancelAfter(), you have to check after the call to verify their was no cancellation before or during the call causing CancelAfter() to no-op. This is demonstrated in the second HttpClient usage example.