通过AEC解读WF的核心原理(四)AEC在内部执行childActivity
说明
Activity有两种。一种继承自System.Workflow.ComponentModel.Activity,另一种继承自System.Workflow.ComponentModel.CompositeActivity
CompositeActivity是可以有childActivity,
CompositeActivity通过AEC可以对childActivity的执行进行管理
本例演示了:
如何使用CompositeActivity执行ChildActivity,并在ChildActivity执行完成后将自已关闭
本例下载:
https://files.cnblogs.com/wxwinter/aec4.rar
本例涉及到如下内容:
ActivityExecutionContext.ExecuteActivity(childActivity) 方法
- CompositeActivity调用ExecuteActivity方法执行ChildActivity
- 只的 ChildActivity处于Initialized状态时才能调用该方法
- 引擎会自动执行ChildActivity下面的所有ChildActivity
ActivityExecutionContext.CloseActivity() 方法
- 将当前执行的Activity切换到Closed状态
- 只有在生成的所有执行上下文 (CreateExecutionContext) 都已完成 (CompleteExecutionContext) 时,才能将Activity设置成Closed状态
- 只有在创建完成AEC后才能将Activity设置成Closed状态
- 只有所有ChildActivity都处于 Closed 或 Initialized 状态时,才能将Activity设置成Closed状态
Activity.Closed事件
在 Activity 完成执行时发生
Activity.EnabledActivities
- Activities 的只读子集合
- 添加到Activities集合中,且Enabled 设为真的Activitie
- 取消(CancellationHandlerActivity)这类Activitie,为会出现在该集合中
Activity.RegisterForStatusChange
为状态更改事件注册指定的事件
Activity.UnregisterForStatusChange
注销状态更改事件的指定
ActivityExecutionStatus执行状态
IActivityEventListener<ActivityExecutionStatusChangedEventArgs>接口
监听事件的对象的接口,要实现OnEvent(发生订阅事件时的处理方法)
例1:AEC.ExecuteActivity方法执行childActivity
自定义Activity
public class Activity1 : SequenceActivity { public Activity1() { InitializeComponent(); }
[System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; this.codeActivity2 = new System.Workflow.Activities.CodeActivity(); this.codeActivity1 = new System.Workflow.Activities.CodeActivity(); // // codeActivity2 // this.codeActivity2.Name = "codeActivity2"; this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity2_ExecuteCode); // // codeActivity1 // this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode); // // Activity1 // this.Activities.Add(this.codeActivity1); this.Activities.Add(this.codeActivity2); this.Name = "Activity1"; this.CanModifyActivities = false;
}
private CodeActivity codeActivity2; private CodeActivity codeActivity1;
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { executionContext.ExecuteActivity(this.codeActivity2);
executionContext.ExecuteActivity(this.codeActivity1);
return ActivityExecutionStatus.Executing;
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity1_ExecuteCode"); }
private void codeActivity2_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity2_ExecuteCode"); } } |
测试用工作流
public class Workflow1: SequentialWorkflowActivity { private Activity1 activity11;
public Workflow1() { InitializeComponent(); } [System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; this.activity11 = new wxwinterAecTest.Activity1(); // // activity11 // this.activity11.Name = "activity11"; // // Workflow1 // this.Activities.Add(this.activity11); 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"); }
} |
运行结果
例2:Activity.Closed事件通知父容器执行完成
自定义Activity
public class Activity1 : SequenceActivity { public Activity1() { InitializeComponent(); }
[System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; this.codeActivity2 = new System.Workflow.Activities.CodeActivity(); this.codeActivity1 = new System.Workflow.Activities.CodeActivity(); // // codeActivity2 // this.codeActivity2.Name = "codeActivity2"; this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity2_ExecuteCode); // // codeActivity1 // this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode); // // Activity1 // this.Activities.Add(this.codeActivity1); this.Activities.Add(this.codeActivity2); this.Name = "Activity1"; this.CanModifyActivities = false;
}
private CodeActivity codeActivity2; private CodeActivity codeActivity1;
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { this.codeActivity2.Closed += new EventHandler<ActivityExecutionStatusChangedEventArgs>(codeActivity_Closed); this.codeActivity1.Closed += new EventHandler<ActivityExecutionStatusChangedEventArgs>(codeActivity_Closed);
executionContext.ExecuteActivity(this.codeActivity2); executionContext.ExecuteActivity(this.codeActivity1);
return ActivityExecutionStatus.Executing;
}
void codeActivity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e) { e.Activity.Closed -= new EventHandler<ActivityExecutionStatusChangedEventArgs>(codeActivity_Closed);
foreach (var v in this.EnabledActivities) { if (v.ExecutionStatus != ActivityExecutionStatus.Closed) { return; } }
ActivityExecutionContext aec = sender as ActivityExecutionContext; aec.CloseActivity(); }
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity1_ExecuteCode"); }
private void codeActivity2_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity2_ExecuteCode"); } } |
运行结果
例3:IActivityEventListener<ActivityExecutionStatusChangedEventArgs>接口通知父容器执行完成
自定义Activity
public class Activity1 : SequenceActivity, IActivityEventListener<ActivityExecutionStatusChangedEventArgs> { public Activity1() { InitializeComponent(); }
[System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; this.codeActivity2 = new System.Workflow.Activities.CodeActivity(); this.codeActivity1 = new System.Workflow.Activities.CodeActivity(); // // codeActivity2 // this.codeActivity2.Name = "codeActivity2"; this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity2_ExecuteCode); // // codeActivity1 // this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode); // // Activity1 // this.Activities.Add(this.codeActivity1); this.Activities.Add(this.codeActivity2); this.Name = "Activity1"; this.CanModifyActivities = false;
}
private CodeActivity codeActivity2; private CodeActivity codeActivity1;
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { this.codeActivity1.RegisterForStatusChange(Activity.ClosedEvent, this); this.codeActivity2.RegisterForStatusChange(Activity.ClosedEvent, this);
executionContext.ExecuteActivity(this.codeActivity2); executionContext.ExecuteActivity(this.codeActivity1);
return ActivityExecutionStatus.Executing; }
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity1_ExecuteCode"); }
private void codeActivity2_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("codeActivity2_ExecuteCode"); }
void IActivityEventListener<ActivityExecutionStatusChangedEventArgs>.OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e) { e.Activity.UnregisterForStatusChange(Activity.ClosedEvent, this); foreach (var v in this.EnabledActivities) { if (v.ExecutionStatus != ActivityExecutionStatus.Closed) { return; } }
ActivityExecutionContext aec = sender as ActivityExecutionContext; aec.CloseActivity(); } } |
运行结果