Datasheet
Chapter 1: Workfl ow Programming Principles
18
SequenceActivity
Here, I ’ ll walk you through the implementation of the logical { } control flow construct, which does
exactly what the C# { } flow control construct does. That is, it executes its constituent child activities in
sequential linear fashion. This implementation provides you with a simplified duplicate of the standard
WF SequenceActivity activity, as shown in Listing 1 - 7 .
Listing 1 - 7: The SequenceActivity activity
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System;
namespace Chapter1
{
public class SequenceActivity : CompositeActivity
{
protected override ActivityExecutionStatus Execute(
ActivityExecutionContext executionContext)
{
if (this.EnabledActivities.Count == 0)
return ActivityExecutionStatus.Closed;
Activity activity = this.EnabledActivities[0];
activity.Closed += Activity_Closed;
executionContext.ExecuteActivity(activity);
return ActivityExecutionStatus.Executing;
}
void Activity_Closed(object sender,
ActivityExecutionStatusChangedEventArgs e)
{
e.Activity.Closed -= this.Activity_Closed;
ActivityExecutionContext executionContext =
sender as ActivityExecutionContext;
int index = this.EnabledActivities.IndexOf(e.Activity);
index++;
if (index == this.EnabledActivities.Count)
executionContext.CloseActivity();
else
{
Activity activity = this.EnabledActivities[index];
activity.Closed += this.Activity_Closed;
executionContext.ExecuteActivity(activity);
}
}
}
}
The SequenceActivity activity, just like any other control flow activity, inherits from the
CompositeActivity base class and overrides its Execute method. The SequenceActivity ’ s implementation
of the Execute method first checks whether the EnabledActivities collection is empty. Recall that this
collection contains the child activities of the composite activity. If the collection is empty, then the
c01.indd 18c01.indd 18 8/25/08 4:02:56 PM8/25/08 4:02:56 PM