通过AEC解读WF的核心原理(六)创建复本ForEach循环

说明

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

 

一个ForEach的例子,与Replicator功能类似。

本例主要演示如何创建Activity的执行副本

 

 

本例知识点:

 

ActivityExecutionContextManager:该类允许CompositeActivity创建和管理childActivity的AEC

属性

ExecutionContexts

获取由当前Activity创建的AEC集合

PersistedExecutionContexts

获取由当前Activity创建的已经完成并被持久化的AEC集合。

属性值Guid集合描述所有被持久化的AEC集合。

 

方法

CompleteExecutionContext

标记AEC已经完成。将完成的AEC存放在ExecutionContexts属性中。工作流引擎只允许ActivityClosed状态中才能执行成功。forcePersist参数是说明是否将指定的AEC持久化。被持久化后的AEC可以被恢复。该属性默认值为false

CreateExecutionContext

创建指定ActivityAEC

GetExecutionContext

获取与指定Activity相关联的第一个AEC。因为一个Activity可能存在多个AEC

GetPersistedExecutionContext

获取与指定Guid相应的被持久化的AEC。获取的AEC将被存放在ExecutionContexts属性中并在PersistedExecutionContexts属性中移除被获取的AEC

 

 

 

例子

SendMessag的Activity

public class SendMessage:Activity

    {

 

 

public string Message

{

get { return (string)GetValue(MessageProperty); }

set { SetValue(MessageProperty, value); }

}

 

public static readonly DependencyProperty MessageProperty =DependencyProperty.Register("Message", typeof(string), typeof(SendMessage), new PropertyMetadata(""));

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

System.Console.WriteLine(this.Message);

return ActivityExecutionStatus.Closed;

}

    }

 

ForEach的Activity

public class ForEach: SequenceActivity

{

public static readonly DependencyProperty DataItemsProperty = DependencyProperty.Register("DataItems", typeof(String[]), typeof(ForEach));

public String[] DataItems

{

get { return (String[])GetValue(DataItemsProperty); }

set { SetValue(DataItemsProperty, value); }

}

int i=0;

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

if (EnabledActivities.Count == 0 || DataItems == null || DataItems.Count() == 0)

{

return ActivityExecutionStatus.Closed;

}

if (EnabledActivities[0] is SendMessage)

{

ExecuteDataItem(executionContext);

return ActivityExecutionStatus.Executing;

}

return base.Execute(executionContext);

}

 

void activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

e.Activity.Closed -= activity_Closed;

ActivityExecutionContext aec = sender as ActivityExecutionContext;

ActivityExecutionContext childContext = aec.ExecutionContextManager.GetExecutionContext(e.Activity);

aec.ExecutionContextManager.CompleteExecutionContext(childContext);

if(i==DataItems.Count()-1)

{

aec.CloseActivity();

}

else

{

i = i + 1;

ExecuteDataItem(aec);

}

}

 

private void ExecuteDataItem(ActivityExecutionContext executionContext)

{

Activity activity = EnabledActivities[0];

ActivityExecutionContext newContext = executionContext.ExecutionContextManager.CreateExecutionContext(activity);

newContext.Activity.Closed += activity_Closed;

newContext.Activity.SetValue(SendMessage.MessageProperty, DataItems[i]);

newContext.ExecuteActivity(newContext.Activity);

}

}

 

测试用工作流

public class Workflow1: SequentialWorkflowActivity

{

private SendMessage sendMessage1;

private ForEach forEach1;

 

        public Workflow1()

        {

            InitializeComponent();

        }

[System.Diagnostics.DebuggerNonUserCode]

private void InitializeComponent()

{

this.CanModifyActivities = true;

this.sendMessage1 = new wxwinterAecTest.SendMessage();

this.forEach1 = new wxwinterAecTest.ForEach();

//

// sendMessage1

//

this.sendMessage1.Message = "";

this.sendMessage1.Name = "sendMessage1";

//

// forEach1

//

this.forEach1.Activities.Add(this.sendMessage1);

this.forEach1.DataItems = new string[] {

"wxd",

"wxwinter",

"lzm"};

this.forEach1.Name = "forEach1";

//

// Workflow1

//

this.Activities.Add(this.forEach1);

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");

}

 

}

 

运行结果

 

 

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