Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

How to: Create a State Machine 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 StateMachine activity, and the custom activities from the previous How to: Create an Activity topic. The workflow models a number guessing game.

Gg983472.note(en-us,VS.100).gifNote:
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.

Gg983472.note(en-us,VS.100).gifNote:
State machine functionality in Windows Workflow Foundation (WF) was introduced as part of the Update 4.0.1 for Microsoft .NET Framework 4 (KB2478063). To create state machine workflow applications in Visual Studio 2010, the Update 4.0.1 for Microsoft .NET Framework 4 – Design-time Package for Visual Studio 2010 SP1 (KB2495593) is required. For more information and instructions on how to download and configure these updates, see Update 4.0.1 for Microsoft .NET Framework 4 Features.

Gg983472.note(en-us,VS.100).gifNote:
To view a video walkthrough of this topic or to download a completed version of the tutorial, see Windows Workflow Foundation (WF4) - Getting Started Tutorial.

To create the workflow project

  1. Open the solution from the previous How to: Create an Activity topic by using Visual Studio 2010.

    Gg983472.note(en-us,VS.100).gifNote:
    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.

  2. Right-click the GettingStartedTutorial solution in Solution Explorer and select Add, New Project.

    Gg983472.Tip(en-us,VS.100).gifTip:
    If the Solution Explorer window is not displayed, select Solution Explorer from the View menu.

  3. In the Installed Templates list, select Visual C#, Workflow (or Visual Basic, Workflow).

    Gg983472.note(en-us,VS.100).gifNote:
    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.

    Select .NET Framework 4 Platform Update 1 (KB2478063) in the .NET Framework version drop-down list.

    New Project

    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.

  4. Right-click the newly added WorkflowConsoleApplication1 in Solution Explorer and select Add Reference. Select ActivityLibrary1 from the Projects tab and click OK.

  5. 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

  1. If you skipped the preceding To create the workflow project section because the project was already created for one of the other tutorial steps, then you must set the Target framework. Right-click WorkflowConsoleApplication1 in Solution Explorer and select Properties. In the Target framework drop-down, select .NET Framework 4 Client Profile Platform Update 1 (KB2478063), and click Yes to confirm.

    Project Properties
    Gg983472.note(en-us,VS.100).gifNote:
    The .NET Framework 4 Client Profile is a subset of the .NET Framework 4 that is optimized for client applications. The .NET Framework 4 Client Profile Platform Update 1 (KB2478063) is the corresponding subset of the .NET Framework 4 Platform Update 1 (KB2478063). For more information on client profiles, see .NET Framework Client Profile.

  2. Right-click WorkflowConsoleApplication1 in Solution Explorer and select Add, New Item.

  3. In the installed templates list, select Workflow. Select Activity from the Workflow list.

  4. Type StateMachineNumberGuessWorkflow into the Name box and click Add.

  5. Drag a StateMachine activity from the State Machine section of the Toolbox and drop it on the workflow design surface.

    Gg983472.note(en-us,VS.100).gifNote:
    If the state machine activities do not appear in the Toolbox, ensure that the Target framework is set to one of the .NET Framework 4 Platform Update 1 profiles as specified in step 1 of this procedure.

To create the workflow variables and arguments

  1. Double-click StateMachineNumberGuessWorkflow.xaml in Solution Explorer to display the workflow in the designer, if it is not already displayed.

  2. Click Arguments in the lower-left side of the workflow designer to display the Arguments pane.

  3. Click Create Argument.

  4. 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.

  5. Click Create Argument.

  6. 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.

  7. Click Arguments in the lower-left side of the activity designer to close the Arguments pane.

  8. Click Variables in the lower-left side of the workflow designer to display the Variables pane.

  9. Click Create Variable.

    Gg983472.Tip(en-us,VS.100).gifTip:
    If no Create Variable box is displayed, click the StateMachine activity on the workflow designer surface to select it.

  10. Type Guess into the Name box, select Int32 from the Variable type drop-down list, and then press ENTER to save the variable.

  11. Click Create Variable.

  12. Type Target into the Name box, select Int32 from the Variable type drop-down list, and then press ENTER to save the variable.

  13. Click Variables in the lower-left side of the activity designer to close the Variables pane.

To add the workflow activities

  1. Click State1 to select it. In the Properties Window, change the DisplayName to Initialize Target.

    Gg983472.Tip(en-us,VS.100).gifTip:
    If the Properties Window is not displayed, select Properties Window from the View menu.

  2. Double-click the newly renamed Initialize Target state in the workflow designer to expand it.

  3. Drag an Assign activity from the Primitives section of the Toolbox and drop it onto the Entry section of the state. Type Target into the To box and the following expression into the Enter a VB expression box.

    New System.Random().Next(1, MaxNumber + 1)
    
    Gg983472.Tip(en-us,VS.100).gifTip:
    If the Toolbox window is not displayed, select Toolbox from the View menu.

  4. Return to the overall state machine view in the workflow designer by clicking StateMachine in the breadcrumb display at the top of the workflow designer.

  5. Drag a State activity from the State Machine section of the Toolbox onto the workflow designer and hover it over the Initialize Target state. Note that four triangles will appear around the Initialize Target state when the new state is over it. Drop the new state on the triangle that is immediately below the Initialize target state. This places the new state onto the workflow and creates a transition from the Initialize Target state to the new state.

  6. Click State1 to select it, change the DisplayName to Enter Guess, and then double-click the state in the workflow designer to expand it.

  7. Drag a WriteLine activity from the Primitives section of the Toolbox and drop it onto the Entry section of the state.

  8. Type the following expression into the Text property box of the WriteLine.

    "Please enter a number between 1 and " & MaxNumber
    
  9. Drag an Assign activity from the Primitives section of the Toolbox and drop onto the Exit section of the state.

  10. Type Turns into the To box and Turns + 1 into the Enter a VB expression box.

  11. Return to the overall state machine view in the workflow designer by clicking StateMachine in the breadcrumb display at the top of the workflow designer.

  12. Drag a FinalState activity from the State Machine section of the Toolbox, hover it over the Enter Guess state, and drop it onto the triangle that appears to the right of the Enter Guess state so that a transition is created between Enter Guess and Final State.

  13. The default name of the transition is T2. Click the transition in the workflow designer to select it, and set its DisplayName to Guess Correct. Then click and select the Final State, and drag it to the right so that there is room for the full transition name to be displayed without overlaying either of the two states. This will make it easier to complete the remaining steps in the tutorial.

  14. Double-click the newly renamed Guess Correct transition in the workflow designer to expand it.

  15. Drag a ReadInt activity from the ActivityLibrary1 section of the Toolbox and drop it in the Trigger section of the transition.

  16. In the Properties Window for the ReadInt activity, type "EnterGuess" including the quotes into the BookmarkName property value box, and type Guess into the Result property value box

  17. Type the following expression into the Guess Correct transition’s Condition property value box.

    Guess = Target
    
  18. Return to the overall state machine view in the workflow designer by clicking StateMachine in the breadcrumb display at the top of the workflow designer.

    Gg983472.note(en-us,VS.100).gifNote:
    A transition occurs when the trigger event is received and the Condition, if present, evaluates to True. For this transition, if the user’s Guess matches the randomly generated Target, then control passes to the Final State and the workflow completes.

  19. Depending on whether the guess is correct, the workflow should transition either to the Final State or back to the Enter Guess state for another try. Both transitions share the same trigger of waiting for the user’s guess to be received via the ReadInt activity. This is called a shared transition. To create a shared transition, click the circle that indicates the start of the Guess Correct transition and drag it to the desired state. In this case the transition is a self-transition, so drag the start point of the Guess Correct transition and drop it back onto the bottom of the Enter Guess state. After creating the transition, set its DisplayName property to Guess Incorrect.

  20. Double-click the Guess Incorrect transition in the workflow designer to expand it. Note that the Trigger is already set to the same ReadInt activity that was used by the Guess Correct transition.

  21. Type the following expression into the Condition property value box.

    Guess <> Target
    
  22. Drag an If activity from the Control Flow section of the Toolbox and drop it in the Action section of the transition.

  23. Type the following expression into the If activity’s Condition property value box.

    Guess < Target
    
  24. Drag two WriteLine activities from the Primitives section of the Toolbox and drop them so that one is in the Then section of the If activity, and one is in the Else section.

  25. 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."
    
  26. 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."
    
  27. Return to the overall state machine view in the workflow designer by clicking StateMachine in the breadcrumb display at the top of the workflow designer.

    The following example illustrates the completed workflow.

    Completed State Machine Workflow

To build the workflow

  1. 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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.