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.
精彩评论