thumbmunkeys
thumbmunkeys

Reputation: 20764

BlockingCollection and CancellationToken passing

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?

Upvotes: 1

Views: 1426

Answers (2)

Mike Liddell
Mike Liddell

Reputation: 2041

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.

Upvotes: 2

Henk Holterman
Henk Holterman

Reputation: 273244

Why would using the ctor be better?

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

Upvotes: 2

Related Questions