一个ForEach的例子,与Replicator功能类似。 本例主要演示如何创建Activity的执行副本 本例知识点: ActivityExecutionContextManager:该类允许CompositeActivity创建和管理childActivity的AEC

说明

例子下载:

 

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

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

 

 

本例知识点:

 

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

属性

ExecutionContexts

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

PersistedExecutionContexts

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

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

 

方法

CompleteExecutionContext

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

CreateExecutionContext

创建指定Activity的AEC。

GetExecutionContext

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

GetPersistedExecutionContext

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

 

 

 

例子

SendMessag的Activity

java forEach 跳过当次循环_java forEach 跳过当次循环

public								class								SendMessage:Activity
    {
 
 
public								string
        {
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
        {
Console.WriteLine(this.Message);
return								ActivityExecutionStatus.Closed;
        }
    }

 

ForEach的Activity

java forEach 跳过当次循环_java forEach 跳过当次循环_02

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
protected								override								ActivityExecutionStatus Execute(ActivityExecutionContext
        {
if (EnabledActivities.Count == 0 || DataItems == null
            {
return								ActivityExecutionStatus.Closed;
            }
if (EnabledActivities[0] is								SendMessage)
            {
                ExecuteDataItem(executionContext);
return								ActivityExecutionStatus.Executing;
            }
return								base.Execute(executionContext);
        }
 
void activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs
        {
            e.Activity.Closed -= activity_Closed;           
ActivityExecutionContext aec = sender as								ActivityExecutionContext;
ActivityExecutionContext
            aec.ExecutionContextManager.CompleteExecutionContext(childContext);  
if(i==DataItems.Count()-1)
            {
                aec.CloseActivity(); 
            }
else
            {
                i = i + 1;
                ExecuteDataItem(aec);
            }
        }
 
private								void ExecuteDataItem(ActivityExecutionContext
        {
Activity
ActivityExecutionContext
            newContext.Activity.Closed += activity_Closed;
SendMessage.MessageProperty, DataItems[i]);
            newContext.ExecuteActivity(newContext.Activity);            
        }
    }

 

测试用工作流

public								class								Workflow1: SequentialWorkflowActivity
    {
private								SendMessage
private								ForEach
     
public
        {
            InitializeComponent();
        }
DebuggerNonUserCode]
private								void
        {
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);
 = "Workflow1";
this.CanModifyActivities = false;
 
        }
    }

 

宿主

class								Program
    {
static								void
        {
WorkflowRuntime workflowRuntime = new								WorkflowRuntime();
new								EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
new								EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);
new								EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
            instance.Start();
 
Console.Read();
 
        }
 
static								void  workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs
        {
Console.WriteLine("WorkflowIdled");
        }
 
static								void  workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs
        {
Console.WriteLine("Terminated"
        }
 
static								void  workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs
        {
Console.WriteLine("WorkflowCompleted");
        }
 
    }

 

运行结果