How to: Create a Sequential Workflow
This topic applies to Windows Workflow Foundation 4 (WF4).
Workflows can be constructed from built-in activities as well as from custom activities. This topic steps through creating a workflow that uses both built-in activities such as the Sequence activity, and the custom activities from the previous How to: Create an Activity topic. The workflow models a number guessing game.
Note
Each topic in the Getting Started tutorial depends on the previous topics. To complete this topic, you must first complete How to: Create an Activity.
Note
To download a completed version of the tutorial, see Windows Workflow Foundation (WF4) - Getting Started Tutorial.
To create the workflow project
Open the solution from the previous How to: Create an Activity topic by using Visual Studio 2010.
Note
The steps to create the workflow project are the same regardless of which style of workflow you decide to create for the tutorial, and only need to be completed once. If you have already completed this step for one of the other styles of workflow, then skip ahead to the next section, To create the workflow.
Right-click the GettingStartedTutorial solution in Solution Explorer and select Add, New Project.
Tip: If the Solution Explorer window is not displayed, select Solution Explorer from the View menu. In the Installed Templates list, select Visual C#, Workflow (or Visual Basic, Workflow).
Note
Depending on which programming language is configured as the primary language in Visual Studio, the Visual C# or Visual Basic node may be under the Other Languages node in the Installed Templates list.
Ensure that .NET Framework 4 is selected in the .NET Framework version drop-down list. Select Workflow Console Application from the Workflow list. Keep the default settings and click OK. This creates a starter workflow application with basic workflow hosting support. In this topic, the workflow is populated with activities. In the next topic, How to: Run a Workflow, the basic hosting code is modified and used to run the workflow application.
Right-click the newly added WorkflowConsoleApplication1 in Solution Explorer and select Add Reference. Select ActivityLibrary1 from the Projects tab and click OK.
Press CTRL+SHIFT+B to build the solution. This adds the custom activities from ActivityLibrary1 to the Toolbox so they can be used in this workflow.
To create the workflow
Right-click WorkflowConsoleApplication1 in Solution Explorer and select Add, New Item.
In the installed templates list, select Workflow. Select Activity from the Workflow list.
Type SequentialNumberGuessWorkflow into the Name box and click Add.
Drag a Sequence activity from the Control Flow section of the Toolbox and drop it on the workflow design surface.
To create the workflow variables and arguments
Double-click SequentialNumberGuessWorkflow.xaml in Solution Explorer to display the workflow in the designer, if it is not already displayed.
Click Arguments in the lower-left side of the workflow designer to display the Arguments pane.
Click Create Argument.
Type MaxNumber into the Name box, select In from the Direction drop-down list, select Int32 from the Argument type drop-down list, and then press ENTER to save the argument.
Click Create Argument.
Type Turns into the Name box that is below the newly added
MaxNumber
argument, select Out from the Direction drop-down list, select Int32 from the Argument type drop-down list, and then press ENTER.Click Arguments in the lower-left side of the activity designer to close the Arguments pane.
Click Variables in the lower-left side of the workflow designer to display the Variables pane.
Click Create Variable.
Tip: If no Create Variable box is displayed, click the Sequence activity on the workflow designer surface to select it. Type Guess into the Name box, select Int32 from the Variable type drop-down list, and then press ENTER to save the variable.
Click Create Variable.
Type Target into the Name box, select Int32 from the Variable type drop-down list, and then press ENTER to save the variable.
Click Variables in the lower-left side of the activity designer to close the Variables pane.
To add the workflow activities
Drag an Assign activity from the Primitives section of the Toolbox and drop it onto the Sequence activity. Type Target into the To box and the following expression into the Enter a VB expression box.
New System.Random().Next(1, MaxNumber + 1)
Tip: If the Toolbox window is not displayed, select Toolbox from the View menu. Drag a DoWhile activity from the Control Flow section of the Toolbox and drop it on the workflow so that it is below the Assign activity.
Type the following expression into the DoWhile activity’s Condition property value box.
Guess <> Target
A DoWhile activity executes its child activities and then evaluates its Condition. If the Condition evaluates to True, then the activities in the DoWhile execute again. In this example, the user’s guess is evaluated and the DoWhile continues until the guess is correct.
Drag a Sequence from the Control Flow section of the Toolbox and drop it in the DoWhile activity.
Drag a Prompt activity from the ActivityLibrary1 section of the Toolbox and drop it in the Sequence activity from the previous step.
In the Properties Window, type "EnterGuess" including the quotes into the BookmarkName property value box for the Prompt activity. Type Guess into the Result property value box, and type the following expression into the Text property box.
"Please enter a number between 1 and " & MaxNumber
Tip: If the Properties Window is not displayed, select Properties Window from the View menu. Drag an Assign activity from the Primitives section of the Toolbox and drop it in the Sequence activity so that it follows the Prompt activity.
Type Turns into the To box and Turns + 1 into the Enter a VB expression box.
Drag an If activity from the Control Flow section of the Toolbox and drop it in the Sequence activity so that it follows the newly added Assign activity.
Type the following expression into the If activity’s Condition property value box.
Guess <> Target
Drag another If activity from the Control Flow section of the Toolbox and drop it in the Then section of the first If activity.
Type the following expression into the newly added If activity’s Condition property value box.
Guess < Target
Drag two WriteLine activities from the Primitives section of the Toolbox and drop them so that one is in the Then section of the newly added If activity, and one is in the Else section.
Click the WriteLine activity in the Then section to select it, and type the following expression into the Text property value box.
"Your guess is too low."
Click the WriteLine activity in the Else section to select it, and type the following expression into the Text property value box.
"Your guess is too high."
The following example illustrates the completed workflow.
To build the workflow
Press CTRL+SHIFT+B to build the solution.
For instructions on how to run the workflow, please see the next topic, How to: Run a Workflow.
See Also
Tasks
How to: Create an Activity
How to: Run a Workflow
Reference
Other Resources
Windows Workflow Foundation Programming
Designing Workflows
Getting Started Tutorial