通过AEC解读WF的核心原理(十)取消与取消处理器

说明

例子下载:

https://files.cnblogs.com/wxwinter/aec10_1.rar

https://files.cnblogs.com/wxwinter/aec10_2.rar

 

取消的实现

取消处理器CancellationHandlerActivity的使用

 

 

例子:取消

内容控件Activity

public class 内容控件: Activity

    {

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

this.Invoke(正常业务, new EventArgs());

return ActivityExecutionStatus.Executing;

}

 

protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext)

{

System.Console.WriteLine("取消");

this.Invoke(取消业务, new EventArgs());

return ActivityExecutionStatus.Canceling;

// return ActivityExecutionStatus.Closed;

}

 

void 正常业务(object sender, EventArgs e)

{

System.Console.WriteLine("wxwinter");

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CloseActivity();

}

 

void 取消业务(object sender, EventArgs e)

{

//用于处理取消是否恢复的业务

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CloseActivity();

}

    }

 

容器控件Activity

public class 容器控件: SequenceActivity

    {

        public 容器控件()

        {

            InitializeComponent();

        }

 

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.内容控件1 = new wxwinterAecTest.内容控件();

this.内容控件1.Name = "内容控件1";

this.Activities.Add(this.内容控件1);

this.CanModifyActivities = false;

}

 

private 内容控件 内容控件1;

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

this.内容控件1.Closed += activity11_Closed;

this.内容控件1.Executing+=new EventHandler<ActivityExecutionStatusChangedEventArgs>(activity11_Executing);

executionContext.ExecuteActivity(this.内容控件1);

return ActivityExecutionStatus.Executing;

 

}

 

bool IsCancel = false;

 

void activity11_Executing(object sender, ActivityExecutionStatusChangedEventArgs e)

{

IsCancel = true; //一个模拟要取消的业务情形

if (IsCancel)

{

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CancelActivity(this.内容控件1);

}

}

 

void activity11_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

e.Activity.Closed -= activity11_Closed;

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CloseActivity();

}

    }

 

测试用工作流

public class Workflow1: SequentialWorkflowActivity

{

private 容器控件 容器控件1;

        public Workflow1()

        {

            InitializeComponent();

        }

 

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.容器控件1 = new wxwinterAecTest.容器控件();

//

// 容器控件1

//

this.容器控件1.Name = "容器控件1";

//

// Workflow1

//

this.Activities.Add(this.容器控件1);

this.Name = "Workflow1";

this.CanModifyActivities = false;

}

    }

 

宿主

class Program

{

static void Main()

{

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);

workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));

instance.Start();

 

System.Console.Read();

 

}

 

static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowIdled");

}

 

static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{

System.Console.WriteLine("Terminated" + e.Exception.Message);

}

 

static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{

System.Console.WriteLine("WorkflowCompleted");

}

 

}

 

运行结果

IsCancel = true;

IsCancel = false

 

例子:取消处理器CancellationHandlerActivity

支持取消的控件Activity

public class 支持取消的控件 : SequenceActivity

{

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

this.EnabledActivities[0].Closed +=Activity_Closed;

executionContext.ExecuteActivity(this.EnabledActivities[0]);

return ActivityExecutionStatus.Executing;

}

 

void Activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CloseActivity();

}

 

protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext)

{

foreach (var v in this.Activities)

{

if (v is CancellationHandlerActivity)

{

v.Closed += CancellationHandlerActivity_Closed;

executionContext.ExecuteActivity(v);

return ActivityExecutionStatus.Canceling;

}

}

return ActivityExecutionStatus.Closed;

}

 

void CancellationHandlerActivity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CloseActivity();

}

 

}

 

可取消的容器Activity

public class 可取消的容器 : SequenceActivity

{

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

this.EnabledActivities[0].Closed += activity_Closed;

this.EnabledActivities[0].Executing += activity_Executing;

executionContext.ExecuteActivity(this.EnabledActivities[0]);

return ActivityExecutionStatus.Executing;

}

public bool 是否取消 { get; set; }

void activity_Executing(object sender, ActivityExecutionStatusChangedEventArgs e)

{

if (是否取消)

{

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CancelActivity(this.EnabledActivities[0]);

}

}

 

void activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

e.Activity.Closed -= activity_Closed;

ActivityExecutionContext aec = sender as ActivityExecutionContext;

aec.CloseActivity();

}

}

 

测试用工作流

 

 

 

 

public class Workflow1: SequentialWorkflowActivity

{

private CodeActivity 正常路径业;

private 支持取消的控件 支持取消的控件1;

private CodeActivity 取消业务路径;

private CancellationHandlerActivity cancellationHandlerActivity1;

private 可取消的容器 可取消的容器1;

 

        public Workflow1()

        {

            InitializeComponent();

        }

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.取消业务路径 = new System.Workflow.Activities.CodeActivity();

this.cancellationHandlerActivity1 = new System.Workflow.ComponentModel.CancellationHandlerActivity();

this.正常路径业 = new System.Workflow.Activities.CodeActivity();

this.支持取消的控件1 = new wxwinterAecTest.支持取消的控件();

this.可取消的容器1 = new wxwinterAecTest.可取消的容器();

//

// 取消业务路径

//

this.取消业务路径.Name = "取消业务路径";

this.取消业务路径.ExecuteCode += new System.EventHandler(this.取消业务路径_ExecuteCode);

//

// cancellationHandlerActivity1

//

this.cancellationHandlerActivity1.Activities.Add(this.取消业务路径);

this.cancellationHandlerActivity1.Name = "cancellationHandlerActivity1";

//

// 正常路径业

//

this.正常路径业.Name = "正常路径业";

this.正常路径业.ExecuteCode += new System.EventHandler(this.正常路径业_ExecuteCode);

//

// 支持取消的控件1

//

this.支持取消的控件1.Activities.Add(this.正常路径业);

this.支持取消的控件1.Activities.Add(this.cancellationHandlerActivity1);

this.支持取消的控件1.Name = "支持取消的控件1";

//

// 可取消的容器1

//

this.可取消的容器1.Activities.Add(this.支持取消的控件1);

this.可取消的容器1.Name = "可取消的容器1";

this.可取消的容器1.是否取消 = true;

//

// Workflow1

//

this.Activities.Add(this.可取消的容器1);

this.Name = "Workflow1";

this.CanModifyActivities = false;

}

 

private void 正常路径业_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine("正常执行");

}

 

private void 取消业务路径_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine("取消执行");

}

    }

 

宿主

class Program

{

static void Main()

{

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);

workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));

instance.Start();

 

System.Console.Read();

 

}

 

static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowIdled");

}

 

static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{

System.Console.WriteLine("Terminated" + e.Exception.Message);

}

 

static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{

System.Console.WriteLine("WorkflowCompleted");

}

 

}

 

运行结果

 

 

 

 

posted @ 2008-06-11 13:57  WXWinter(冬)  阅读(1704)  评论(0编辑  收藏  举报