通过AEC解读WF的核心原理(五)实现一个从下向上执行的顺序容器

说明

本例实现了一个从下向上倒着执行的顺序容器,本例并无什么具体的应用场景,只是一个控制子Activity执行的演示,

本例演示了如何按一定规则执行子Activity,并在子Activity执行完成后关闭子Activity,在全部业务完成后关闭自已

例子下载:https://files.cnblogs.com/wxwinter/aec5.rar

 

例子

自定义Activity

    public class Activity1: SequenceActivity

    {

int index;

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

index = this.EnabledActivities.Count - 1;

var v = this.EnabledActivities[index];

v.Closed +=v_Closed;

executionContext.ExecuteActivity(v);

return ActivityExecutionStatus.Executing;

}

 

void v_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

ActivityExecutionContext executionContext = sender as ActivityExecutionContext;

 

e.Activity.Closed -= v_Closed;

index = index - 1;

if (index < 0)

{

executionContext.CloseActivity();

}

else

{

var v = this.EnabledActivities[index];

v.Closed +=v_Closed;

executionContext.ExecuteActivity(v);

}

 

}

    }

 

测试用工作流

public class Workflow1: SequentialWorkflowActivity

{

private CodeActivity codeActivity4;

private CodeActivity codeActivity3;

private CodeActivity codeActivity2;

private CodeActivity codeActivity1;

private Activity1 activity11;

 

        public Workflow1()

        {

            InitializeComponent();

        }

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.codeActivity4 = new System.Workflow.Activities.CodeActivity();

this.codeActivity3 = new System.Workflow.Activities.CodeActivity();

this.codeActivity2 = new System.Workflow.Activities.CodeActivity();

this.codeActivity1 = new System.Workflow.Activities.CodeActivity();

this.activity11 = new wxwinterAecTest.Activity1();

//

// codeActivity4

//

this.codeActivity4.Name = "codeActivity4";

this.codeActivity4.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// codeActivity3

//

this.codeActivity3.Name = "codeActivity3";

this.codeActivity3.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// codeActivity2

//

this.codeActivity2.Name = "codeActivity2";

this.codeActivity2.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// codeActivity1

//

this.codeActivity1.Name = "codeActivity1";

this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity_ExecuteCode);

//

// activity11

//

this.activity11.Activities.Add(this.codeActivity1);

this.activity11.Activities.Add(this.codeActivity2);

this.activity11.Activities.Add(this.codeActivity3);

this.activity11.Activities.Add(this.codeActivity4);

this.activity11.Name = "activity11";

//

// Workflow1

//

this.Activities.Add(this.activity11);

this.Name = "Workflow1";

this.CanModifyActivities = false;

 

}

 

private void codeActivity_ExecuteCode(object sender, EventArgs e)

{

System.Console.WriteLine(((Activity)sender).Name);

}

    }

 

宿主

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-10 13:51  WXWinter(冬)  阅读(3292)  评论(7编辑  收藏  举报