BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News .NET 4 Cancellation Framework

.NET 4 Cancellation Framework

This item in japanese

.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

Adoption
Style

BT