Datasheet

Chapter 1: Work ow Programming Principles
20
As Listing 1 - 7 shows, this event handler takes the following steps:
1. It unregisters the event handler:
e.Activity.Closed -= this.Activity_Closed;
2. It casts the first argument to the ActivityExecutionContext type:
ActivityExecutionContext executionContext =
sender as ActivityExecutionContext;
3. It determines the index of the child activity that raised the Closed event and increments the
index:
int index = this.EnabledActivities.IndexOf(e.Activity);
index++;
4. If the child activity that raised the Closed event is the last child activity of the SequenceActivity
activity, it invokes the CloseActivity method on the ActivityExecutionContext object to inform
the workflow run time that the SequenceActivity activity has now completed its execution:
if (index == this.EnabledActivities.Count)
executionContext.CloseActivity();
The SequenceActivity activity, like any other composite activity, notifies the workflow run time
that it has completed its execution only after each and every single child activity is in either
Initialized or Closed state. In general, a composite activity mustn t report that it has completed
its execution if one or more of its child activities are still executing. Imagine how weird it would
be if a C# { } statement block were to complete its execution while one or more program state-
ments inside the block were still executing! If a composite activity attempts to report the
completion of its execution while one or more of its child activities is not in either Closed or
Initialized state, the workflow run time raises an exception. In other words, the workflow run
time does not allow a composite activity to transition to the Closed state until all its child
activities are either in Closed or Initialized state.
You may be wondering how it would be possible for one or more child activities of a composite
activity to remain in the Initialized state. This is very similar to the C# { } statement block that
contains if and else if flow control program statements. Obviously, only the program
statements contained in one of these flow control program statements are executed. Similarly, a
logical { } flow control program statement may contain logical “ if ” and “ else if ” flow control
program statements such that only the child activities of one of these flow control program
statements are bound to execute that is, only the Execute methods of the child activities of
one these two flow control program statements are bound to be executed. The child activities of
the flow control program statements that are not executed will remain in the Initialized state.
Keep in mind that the Initialize methods of all activities, including those that are never executed,
are invoked when the workflow is loaded into memory. As such, all activities are initially in the
Initialized state.
5. If the child activity that raised the Closed event is not the last logical program statement in the
logical { } flow control program statement, the Activity_Closed method takes the same steps
c01.indd 20c01.indd 20 8/25/08 4:02:57 PM8/25/08 4:02:57 PM