.NET 4 Cancellation Framework

by Al Tenhundfeld on Jun 03, 2009 |

.NET 4 will have new types to support building cancellation-aware applications and libraries. The new CancellationToken, CancellationTokenSource, and cancellation exception types provide a cooperative cancellation framework.

the motivating principles that the new types were designed to support:

  • any unit of work is commenced, it should have a consistent means to support early termination in response to a cancellation request.
  • If some unit of work controls various other moving pieces, it should be able to conveniently chain cancellation requests to them.
  • Blocking calls should be able to support cancellation.
  • Calls to complex operations, such as MoveNext() on a PLINQ
  • enumerator, should have simple yet comprehensive cancellation support.
  • When infrastructure makes calls back to potentially long-running user code, it should be possible for the user code to observe and respond to cancellation requests in a cooperative fashion.
  • Cancellation should be an obvious part of an API with clean and consistent semantics.
  • Cancellation should not be forceful but instead cooperative.

The framework is built upon two new types:

  • CancellationToken
  • a struct that represents a ‘potential request for cancellation’. This struct is used as an argument to allow methods to poll on it or register a callback to be fired when cancellation is requested.
  • CancellationTokenSource
  • a class that provides the mechanism for initiating a cancellation request. It also provides an association to the related token. This design allows separation of the the two primary cancellation operations: initiating a cancellation request & observing and responding to cancellation. Specifically, methods that accept only a CancellationToken can observe a cancellation request but cannot initiate one.

These new .NET 4 cancellation types provide a framework for building systems that have robust and consistent cancellation behaviors. This will specifically assist with GUIs, applications, and libraries that manage long running or blocking operations. The long-term goal is to reduce the variation and inconsistencies in the current offering of 3rd-party cancellation solutions. These new types will also offer a uniform contract to interoperate with Parallel Extensions.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.