Datasheet

Chapter 1: Work ow Programming Principles
24
If every single child activity is in either Initialized or Closed state, the event handler calls the CloseActivity
method on the ActivityExecutionContext object to inform the workflow run time that ParallelActivity has
now completed its execution and is ready to transition from the Executing to the Closed state:
ActivityExecutionContext executionContext = sender as ActivityExecutionContext;
executionContext.CloseActivity();
Developing Iterative Flow Control Constructs
In this section, I ll show you how to implement custom iterative flow control constructs that repeatedly
execute their constituent logical program statement, which is normally a SequenceActivity activity. First
you ll learn some basic concepts and techniques that apply to iterative flow control constructs.
Consider the following C# for iterative flow control construct:
for (int i=0; i < 10; i++)
{
int j = 0;
Console.WriteLine(j);
j++;
}
The Console.WriteLine in each iteration will write out 0 regardless of the fact that the previous iteration
incremented j by one. In other words, the j local variable is reset at the beginning of each iteration. This
means that what one iteration does to the local variable j is invisible to other iterations. This is because
each iteration allocates memory for its local variables on the stack, which is disposed of after the
iteration completes.
In general, each iteration of a C# iterative flow control construct such as “ for ” and “ while ” has its own
execution context, which is disposed of at the end of the iteration. This ensures that what happens in one
iteration does not affect other iterations.
Each iteration of a business or logical iterative flow control construct such as for, just like a C# iterative
flow control construct, must have its own execution context, which is normally disposed of at the end of
the iteration. Because each iteration uses a different execution context, no residual effects are carried
from one iteration to another.
As discussed earlier, every time the WF scheduler invokes an execution method, such as the Execute
method of an activity or an event handler registered for the Closed event of an activity, it passes an
ActivityExecutionContext object as the first parameter of the execution method. This object represents
the execution context of the execution method.
This execution context contains the states of the CLR objects that represent the enabled activities of the
current workflow instance, the state of the WF scheduler s work queue that contains the scheduled work
items, and so on. In other words, the execution context captures the essential data stored in the thread ’ s
stack in a normal C# program. Because the execution context of a C# program is stored in the stack of the
thread on which it is running, the program cannot be suspended on one thread in one process on one
machine and resumed where it left off on a different thread in a different process on a different machine.
In WF, conversely, the execution context is explicitly allocated on the heap and serialized to the
underlying durable storage together with the workflow instance.
c01.indd 24c01.indd 24 8/25/08 4:02:58 PM8/25/08 4:02:58 PM