开发者

BlockingCollection and CancellationToken passing

开发者 https://www.devze.com 2023-01-25 13:30 出处:网络
Why is the CancellationToken for a BlockingCollection not passed in the constructor ofBlockingCollection?

Why is the CancellationToken for a BlockingCollection not passed in the constructor ofBlockingCollection?

The token has to be passed into the Take and Add methods, what is 开发者_开发知识库the reason for this?


Why would using the ctor be better?

I can imagine using a CancellationToken for Take() but not for Add().
Or using different tokens.


CancellationToken is used to support "operation cancellation". this is more general concept than a object instance with a boolean field meaning "active/disabled". It supports concurrency, object reuse and other interesting scenarios.

So the operations are Add() and Take() and each of them can be long-running due to the collection being empty or full. If the caller wants the option to cancel a long-running method call, then they pass in a token and signal it if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消