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 ❘ 1311<br />

The WriteLine activity includes a Text property that you can set either on the design surface itself by<br />

simply entering text inline, or by displaying the property grid. Later in this chapter, we show you how to<br />

define your custom activities to use this same behavior.<br />

The Text property is not just a simple string — it’s actually defined as an argument type that can use an<br />

expression as its source. Expressions are evaluated at runtime to yield a result, <strong>and</strong> it is this textual result<br />

that is used as the input to the WriteLine activity. To enter a simple text expression, you must use double<br />

quotation marks — so if you are following along with this in Visual Studio, type “Hello World” into the<br />

Text property. If you omit the quotation marks, you will receive a compiler error, as without quotation<br />

marks this is not a legal expression.<br />

If you build <strong>and</strong> run the program, you will see the output text on the console. When the program<br />

executes, an instance of the workflow is created in the Main method, which uses a static method of<br />

the WorkflowInvoker class to execute the instance. The code for this example is available in the<br />

01_HelloWorld solution.<br />

The WorkflowInvoker class is new to Workflow 4, <strong>and</strong> it lets you synchronously invoke a workflow. There<br />

are two other methods of workflow execution that execute workflows asynchronously, which you’ll see later<br />

in this chapter. Synchronous execution was possible in Workflow 3.x but was somewhat more difficult to set<br />

up, <strong>and</strong> there was a lot more overhead.<br />

The synchronous nature of WorkflowInvoker makes it ideal for running short-lived workflows in response<br />

to some UI action — you could use a workflow here to enable or disable some elements of the UI. While<br />

it was possible to do this in Workflow 3.x, it was also more difficult to synchronously execute a given<br />

workflow instance.<br />

aCTiViTies<br />

Everything in a workflow is an activity, including the workflow itself. The workflow is a specific type of<br />

activity that typically allows other activities to be defined within it — this is known as a composite activity,<br />

<strong>and</strong> you’ll see other composite activities later in this chapter. An activity is just a class that ultimately derives<br />

from the abstract Activity class.<br />

The class hierarchy is somewhat deeper than that defined for Workflow 3.x, <strong>and</strong> the main classes are defined<br />

in Figure 44-3.<br />

Activity<br />

ActivityWithResult<br />

AsyncCodeActivity<br />

CodeActivity<br />

NativeActivity<br />

Activity<br />

AsyncCodeActivity<br />

CodeActivity<br />

NativeActivity<br />

figure 44-3<br />

The Activity class is the root for all workflow activities <strong>and</strong> typically you will derive custom activities<br />

from the second tier. To create a simple activity like the WriteLine activity previously mentioned,<br />

you would derive from CodeActivity, as this class has just enough functionality for your write line<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!