Search Wiki:

WF4 CancellationScope Activity Sample

How do you cancel work in your application? Many middle tier components and services rely on transactions to handle cancellation for them. This makes a great deal of sense because transactional programming is well understood. However there are many times when you must cancel work that cannot be done under a transaction. Cancellation can be very difficult because you have to track work that has been done to know how to cancel it. WF4 also includes compensation activities that can be used to do clean up work as well.

Watch

endpoint.tv - CancellationScope Activity

Read

Windows Workflow Foundation 4 helps you with this by providing a CancellationScope activity.

CancellationScope1.jpg

How does cancellation happen?

Before we consider how the CancellationScope, let’s think about how cancellation happens in a workflow. There are 2 ways it can happen, from inside of a workflow or from the outside. Child workflow activities are scheduled by their parent activity (such as a Sequence, Parallel, Flowchart or some custom activity). The parent activity can decide to 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 cancelled from the outside by the host application by calling WorkflowApplication.Cancel or by returning UnhandledExceptionAction.Cancel from the UnhandledException delegate.

How does the cancellation scope work?

It’s very simple really, the cancellation scope has a Body. This is where you do the work that you might have to cancel later. The CancelHandler contains the activities you want to run if the activity is canceled.

Does an unhandled exception cause the cancellation handler to be invoked?

It depends on what the host application does in response to an unhandled exception.
UnhandledExceptionAction Cancel Handler Invoked Notes
Abort (default) No Workflow aborts WorkflowApplication.Aborted delegate is invoked
Cancel Yes The workflow is canceled as though WorkflowApplication.Cancel() were invoked
Terminate No Specifies that the System.Activities.WorkflowInstance should schedule termination of the root activity and resume execution.

What if my cancellation handler throws an unhandled exception?

If you have an unhandled exception in the cancellation handler the workflow will immediately terminate with the exception. This is similar to throwing an exception from within a catch block.

Parallel Scenario

In this example you can observe how cancellation occurs when a Parallel activity is used.

Parallel.png

Sequential Scenario

In this example you can observe how cancellation occurs when a Sequence is used and the host cancels the activity

Sequence.png

Try It Out

Download the sample code from the Downloads tab. (This sample requires Visual Studio 2010 / .NET 4 )

  1. Explore the sample, check out the code and XAML workflows
  2. Press Ctrl+F5 to compile and run the sample

Test It Out

This sample also includes a unit test project CancelScope.Tests that you can study to see how we tested this sample application

Release 1.5

  • Added parallel demo
  • Modified to show what threads are being used
  • Added extensive updates to unit tests

Release 1.4

  • Modified to show what happens when an unhandled exception is thrown from the body of the CancellationScope activity

Release 1.3

  • Updated to .NET 4 RTM
  • Modified unit tests to remove external dependencies

Release 1.2


Release 1.1

Last edited Aug 2 2010 at 7:15 PM  by RonJacobs, version 16
Comments
Sptelecom wrote  Jul 27 2010 at 8:02 PM  
required callig data

RonJacobs wrote  Aug 2 2010 at 7:16 PM  
huh?

Updating...
Page view tracker