WF4.0 Beta1 CancellationScope 取消容器

WF4.0 Beta1 CancellationScope 取消容器

 

在WF4中,可以使用[Parallel] 并行执行多条分支,当[Parallel]中的所有分支都执行完成后,[Parallel]结束.

在WF4中,可以使用[Pick]实现多条等待分支的单线执行,当一条分支被触发后,其它分支就不会被触发了,当触发的分支完成后,[Pick]结束

但有时我们会的这样一种需求,我们需要并行执行多条分支,当并行分支中的一条或几条分支满足指定条件时,其它正在执行的分支就不执了.同时,为了保证数据的完整性,我们需要在那些可能要取消的分支中余留一组代码.这组代码用于在该分支被取消后做一些收尾工做.

CancellationScope 取消容器

类名: System.Activities.Statements.CancellationScope

基类: NativeActivity

文件: System.Activities.dll

类型: sealed

说明: 1. 在WF4中,可以使用[Parallel] 并行执行多条分支,当[Parallel]中的所有分支都执行完成后,[Parallel]结束.

可以使用[Pick]实现多条等待分支的单线执行,当一条分支被触发后,其它分支就不会被触发了,当触发的分支完成后,[Pick]结束

但有时我们会的这样一种需求,我们需要并行执行多条分支,当并行分支中的一条或几条分支满足指定条件时,其它正在执行的分支就不执了.同时,为了保证数据的完整性,我们需要在那些可能要取消的分支中余留一组代码.这组代码用于在该分支被取消后做一些收尾工做.

2. [CancellationScope]由[Body]与 [CancelHandler] 两部分组成,[Body]为正常执行路径, 如果取消执行会调用 [CancelHandler]中的内容

3. 可以在[Parallel]容器中使用[CancellationScope],当[Parallel]的[CompletionCondition]属性为[True]时,[Parallel]容器会在其内部[CancellationScope]容器执行完成后,结束其它正在执行的并行分支.如果其它正在执行的并行分支是[CancellationScope],则会调用该[CancellationScope]的[CancelHandler]

 

流程

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

宿主

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());

myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e) { System.Console.WriteLine("Completed "); };

 

myInstance.Run();

 

System.Console.Read();

结果

[Parallel][CompletionCondition]属性为[False]

结果

[Parallel][CompletionCondition]属性为[False]

 

 

posted @ 2009-06-20 16:06  WXWinter(冬)  阅读(1132)  评论(3编辑  收藏  举报