Activity有两种。一种继承自System.Workflow.ComponentModel.Activity,另一种继承自System.Workflow.ComponentModel.CompositeActivity
CompositeActivity是可以有childActivity,
CompositeActivity通过AEC可以对childActivity的执行进行管理
本例演示了:
如何使用CompositeActivity执行ChildActivity,并在ChildActivity执行完成后将自已关闭
本例下载:
http://files.cnblogs.com/wxwinter/aec4.rar
本例涉及到如下内容:
在 Activity 完成执行时发生
为状态更改事件注册指定的事件
注销状态更改事件的指定
监听事件的对象的接口,要实现OnEvent(发生订阅事件时的处理方法)
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"); }
} |
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"); } } |
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(); } } |