Using CancellationScope

This sample demonstrates how to use the CancellationScope activity to cancel work in an application.

Many middle tier components and services rely on the well-known programming construct of transactions to handle cancellation for them. However, there are many situations in which work that cannot be done in a transaction must be canceled. Using cancellation is more difficult than using transactions, because work that must be canceled must first be tracked. .NET Framework 4.6.1 helps you with this by providing a CancellationScope activity.

Cancellation can be triggered either from within an activity or from the activity's parent. Child activities are scheduled by their parent activity (such as a Sequence, Parallel, Flowchart, or a custom composite activity). The parent activity can cancel child activities for any reason. For example, a Parallel activity with three child branches will cancel the remaining child branches whenever it completes a branch and the CompletionCondition expression evaluates to true. The workflow can also be canceled externally by the host application by calling Cancel.

To use the CancellationScope activity, put the work that needs to be canceled into the Body property, and put the work that is performed after cancellation into the CancellationHandler property.

This sample demonstrates cancelling an activity from within the activity itself.

The scenario that the sample uses to demonstrate the CancellationScope activity is a client wanting to buy a ticket to Miami as soon as possible. There are two travel agencies that want the business. The sample uses two CancellationScope within a Parallel activity to model this business logic. The CompletionCondition of the Parallel activity is set to true; since the CompletionCondition is checked after any branch completes, this will cause the remaining branch to be canceled after the first branch completes. The client application asks both agencies to buy the ticket, and when the first one confirms that the ticket has been bought, the application cancels the order at the other agency.

To use this sample

  1. Using Visual Studio 2010, open the CancelationScopeXAML.sln solution file.

  2. To build the solution, press CTRL+SHIFT+B.

  3. To run the solution, press CTRL+F5.


The samples may already be installed on your machine. Check for the following (default) directory before continuing.


If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.