InfoQ

News

.NET 4 Cancellation Framework

Posted by Al Tenhundfeld on Jun 03, 2009

Community
.NET
Topics
Transactions Processing ,
.NET Framework
Tags
.NET Framework 4.0 ,
Parallel Programming

.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.

No comments

Watch Thread Reply

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.