15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1314 ❘ ChaPTer 44 windOws wOrkflOw fOundAtiOn 4<br />

At runtime, the Parallel activity schedules each immediate child for execution. The underlying runtime<br />

execution engine will then schedule these children in a FIFO (first in, first out) manner, thereby providing<br />

the illusion of parallel execution; however, they run only on a single thread.<br />

To include true parallel execution, the activities you drop into the Parallel activity must be derived from<br />

the AsyncCodeActivity class. The sample code in 02_ParallelExecution includes an example that shows<br />

how to asynchronously process code within two branches of a Parallel activity. Figure 44-6 shows the use<br />

of two InvokeMethod activities within a Parallel activity.<br />

figure 44-6<br />

The InvokeMethod activities used here call two simple methods, DoJob1 <strong>and</strong> DoJob2, which sleep for two<br />

<strong>and</strong> three seconds, respectively. In order to run these methods asynchronously, there is one final change that<br />

is needed. The InvokeMethod activity has a Boolean RunAsynchronously property that defaults to False.<br />

Setting this in the UI to True then calls the target method asynchronously, thereby allowing the Parallel<br />

activity to execute more than one activity at the same time. With a uni-processor machine, two threads will<br />

execute, giving the illusion of simultaneous execution; however, on a multiprocessor machine, these threads<br />

may well be scheduled on different cores, thereby providing true parallel execution. If you are creating your<br />

own activities, it is worthwhile creating these as asynchronous activities, as then the end user can get the<br />

benefits of true parallel execution.<br />

delay activity<br />

Business processes often need to wait for a period before completing. Consider using a workflow for expense<br />

approval. Your workflow might send an e-mail to your immediate manager asking him or her to approve<br />

your expense claim. The workflow then enters a waiting state where it waits for approval (or, horror of<br />

horrors, rejection); but it would also be nice to define a timeout so that if no response is returned within,<br />

say, one day, the expense claim is then routed to the next manager up the chain of comm<strong>and</strong>.<br />

The Delay activity can form part of this scenario (the other part is the Pick activity defined in the next<br />

section). Its job is to wait for a predefined time before continuing execution of the workflow.<br />

The Delay activity contains a Duration property, which can be set to a discrete TimeSpan value, but, as it<br />

is defined as an expression, this value could be linked to a variable within the workflow or computed from<br />

some other values as required.<br />

When a workflow is executed, it enters an Idle state in which it runs a Delay activity. Workflows that are<br />

idle are c<strong>and</strong>idates for persistence — this is where the workflow instance data is stored within a persistent<br />

medium (such as a SQL Server database), <strong>and</strong> the workflow itself can then be unloaded from memory.<br />

This conserves system resources, as only running workflows need to be in memory at any given time. Any<br />

workflows that are delayed will be persisted to disk.<br />

Pick activity<br />

A common programming construct is to wait for one of a set of possible events — one example of this is the<br />

WaitAny method of the WaitH<strong>and</strong>le class in the System.Threading namespace. The Pick activity is the way<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!