WF4.0 Beta1 CancellationScope cancel container
In WF4, you can use [Parallel] to execute multiple branches in Parallel. When all the branches in [Parallel] are executed, [Parallel] ends.
In WF4, you can use [Pick] to implement the execution of multiple pending branches in a single line. When a branch is triggered, other branches will not be triggered. When the triggered branch is complete, [Pick] ended
But sometimes we need to execute multiple branches in parallel. When one or more branches in the parallel branch meet the specified conditions, other branches that are being executed will not be executed. at the same time, to ensure data integrity, we need to leave a group of code in the branches that may be canceled. this set of code is used to complete some finishing work after the branch is canceled.
CancellationScope cancel container
Class Name: System. Activities. Statements. CancellationScope
Base Class: NativeActivity
File: System. Activities. dll
Type: sealed
Note: 1. In WF4, you can use [Parallel] to execute multiple branches in Parallel. When all the branches in [Parallel] are executed, [Parallel] ends.
You can use [Pick] to implement single-line execution of multiple waiting branches. When one branch is triggered, other branches will not be triggered. When the triggered branch is complete, [Pick] will end.
But sometimes we need to execute multiple branches in parallel. When one or more branches in the parallel branch meet the specified conditions, other branches that are being executed will not be executed. at the same time, to ensure data integrity, we need to leave a group of code in the branches that may be canceled. this set of code is used to complete some finishing work after the branch is canceled.
2. [CancellationScope] consists of [Body] and [CancelHandler]. [Body] is the normal execution path. If the execution is canceled, the content in [CancelHandler] is called.
3. you can use [CancellationScope] in the [Parallel] container. When the [CompletionCondition] attribute of [Parallel] is [True, the [Parallel] container ends other Parallel branches that are being executed after the [CancellationScope] container is executed. if the other parallel branches being executed are [CancellationScope], the [CancelHandler] of the [CancellationScope] will be called.
Example
Process |
<P: Activity
Mc: Ignorable = ""
X: Class = "WorkflowConsoleApplication4.Sequence1" xmlns = "http://schemas.microsoft.com/netfx/2009/xaml/activities/design" xmlns :__ Sequence1 = "clr-namespace: WorkflowConsoleApplication4;" xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns: p = "http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns: sad = "clr-namespace: System. activities. debugger; assembly = System. activities "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml ">
<P: Sequence>
<P: Parallel CompletionCondition = "[True]">
<P: CancellationScope DisplayName = "CancellationScope: A">
<P: CancellationScope. CancelHandler>
<P: WriteLine DisplayName = "WriteLine: Cancel A"> ["Cancel A"] </p: WriteLine>
</P: CancellationScope. CancelHandler>
<P: Sequence>
<P: WriteLine DisplayName = "WriteLine: begin A"> ["begin A"] </p: WriteLine>
<P: Delay DisplayName = "Delay: 5"> [New TimeSpan (0, 0, 5)] </p: Delay>
<P: WriteLine DisplayName = "WriteLine: end A"> ["end A"] </p: WriteLine>
</P: Sequence>
</P: CancellationScope>
<P: CancellationScope DisplayName = "CancellationScope: B">
<P: CancellationScope. CancelHandler>
<P: WriteLine DisplayName = "WriteLine: Cancel B"> ["Cancel B"] </p: WriteLine>
</P: CancellationScope. CancelHandler>
<P: Sequence>
<P: WriteLine DisplayName = "WriteLine: begin B"> ["begin B"] </p: WriteLine>
<P: Delay DisplayName = "Delay: 10"> [New TimeSpan (0, 0, 10)] </p: Delay>
<P: WriteLine DisplayName = "WriteLine: end B"> ["end B"] </p: WriteLine>
</P: Sequence>
</P: CancellationScope>
<P: CancellationScope DisplayName = "CancellationScope: C">
<P: CancellationScope. CancelHandler>
<P: WriteLine DisplayName = "WriteLine: Cancel C"> ["Cancel C"] </p: WriteLine>
</P: CancellationScope. CancelHandler>
<P: Sequence>
<P: WriteLine DisplayName = "WriteLine: begin C"> ["begin C"] </p: WriteLine>
<P: Delay DisplayName = "Delay: 3"> [New TimeSpan (0, 0, 3)] </p: Delay>
<P: WriteLine DisplayName = "WriteLine: end C"> ["end C"] </p: WriteLine>
</P: Sequence>
</P: CancellationScope>
<P: Sequence DisplayName = "Sequence: D">
<P: WriteLine DisplayName = "WriteLine: begin D"> ["begin D"] </p: WriteLine>
<P: Delay DisplayName = "Delay: 8"> [New TimeSpan (0, 0, 8)] </p: Delay>
<P: WriteLine DisplayName = "WriteLine: end D"> ["end D"] </p: WriteLine>
</P: Sequence>
</P: Parallel>
<P: WriteLine DisplayName = "WriteLine: wxwinter"> ["wxwinter"] </p: WriteLine>
</P: Sequence>
</P: Activity> |
Host |
WorkflowInstance myInstance = new WorkflowInstance (new Sequence1 ());
MyInstance. OnCompleted = delegate (WorkflowCompletedEventArgs e) {System. Console. WriteLine ("Completed ");};
MyInstance. Run ();
System. Console. Read (); |
Result |
|
When the [CompletionCondition] attribute of [Parallel] is [False]
Result |
When the [CompletionCondition] attribute of [Parallel] is [False]
|