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.

activities ❘ 1313<br />

The If activity shown in Figure 44-4 also includes a glyph indicating that the Condition property needs to<br />

be defined. This condition is evaluated when the activity is executed; if it returns True, the Then branch will<br />

execute, otherwise the Else branch will be called.<br />

The Condition property is an expression that evaluates to a Boolean value, so you can include any<br />

expression here that is valid.<br />

Workflow 4 includes an expression engine that uses Visual Basic syntax. This might seem strange to a <strong>C#</strong><br />

programmer, as VB is significantly different from <strong>C#</strong>. Unfortunately, that is the way it is at the moment, so<br />

to use the built-in activities you’ll have to learn enough VB to get by. Just remember to be extremely verbose<br />

<strong>and</strong> leave off any semicolons <strong>and</strong> you’ll be OK.<br />

An expression can reference any variables defined in the workflow <strong>and</strong> also access many static classes<br />

available in the .<strong>NET</strong> framework. So you could, for example, define an expression based on the<br />

Environment.Is64BitOperatingSystem value, if that was crucial to some part of your workflow.<br />

Naturally, you can define arguments that are passed into the workflow <strong>and</strong> that can then be evaluated<br />

by an expression inside an If activity. We’ll cover arguments <strong>and</strong> variables later in the chapter.<br />

invokemethod activity<br />

This is one of the most useful activities in the box, as it allows you to execute code that already exists <strong>and</strong><br />

effectively wrap that code within the execution semantics of a workflow.<br />

It’s typical to have a lot of preexisting code, <strong>and</strong> this activity allows you to call that code directly from<br />

within a workflow.<br />

There are two ways that you can use InvokeMethod to call code; which method you use depends on<br />

whether you wish to call a static method or an instance method. If you’re calling a static method, you need<br />

to define the TargetType <strong>and</strong> the MethodName parameters. However if you’re calling an instance method,<br />

then the TargetObject <strong>and</strong> MethodName properties are used, <strong>and</strong> in this instance the TargetObject could<br />

be created inline, or it could be a variable defined somewhere within the workflow itself. The example code<br />

in the 02_ParallelExecution sample shows both modes of using the InvokeMethod activity.<br />

If you need to pass arguments to the method you’re invoking, you can define these using the Parameters<br />

collection. The order of the parameters in the collection must match the order of the parameters to the<br />

method. In addition, there is a Result property that is set to the return value of the function call. You can<br />

bind this to a variable within the workflow in order to use the value as appropriate.<br />

Parallel activity<br />

The Parallel activity is rather poorly named because, at first sight, you might think that on a<br />

multiprocessor machine this activity would schedule its children in true parallel; however, that isn’t the case<br />

apart from some special circumstances.<br />

After you drop a Parallel activity onto the design surface, you can then drop in other subordinate<br />

activities, as shown in Figure 44-5.<br />

figure 44-5<br />

These child activities can be singular activities, as in Figure 44-5, or they can form a composite activity,<br />

such as a Sequence or another Parallel activity.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!