How to: Create a Flowchart 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 Flowchart 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 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.

    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.

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

    Gg983474.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).

    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.

  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. Right-click WorkflowConsoleApplication1 in Solution Explorer and select Add, New Item.

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

  3. Type FlowchartNumberGuessWorkflow into the Name box and click Add.

  4. Drag a Flowchart activity from the Flowchart section of the Toolbox and drop it on the workflow design surface.

To create the workflow variables and arguments

  1. Double-click FlowchartNumberGuessWorkflow.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.

    Gg983474.Tip(en-us,VS.100).gifTip:
    If no Create Variable box is displayed, click the Flowchart 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. Drag an Assign activity from the Primitives section of the Toolbox and drop it onto the workflow so it is below the start node, which is represented by the green circle, at the top of the workflow. Type Target into the To box and the following expression into the Enter a VB expression box.

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

  2. To indicate that the Assign activity is the starting point of the Flowchart hover the mouse over the start node at the top of the workflow. Click one of the rectangles that appear when the mouse is over the start node and drag the mouse down to the top of the Assign activity. When the mouse is over the Assign activity, four rectangles appear. Drag the mouse so that the connecting line from the bottom of the start node connects to the top rectangle of the Assign activity, and then release the mouse button.

  3. Drag a Prompt activity from the ActivityLibrary1 section of the Toolbox and drop it below the Assign activity from the previous step.

  4. In the Properties Window, type "EnterGuess" including the quotes into the BookmarkName property value box. 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
    
    Gg983474.Tip(en-us,VS.100).gifTip:
    If the Properties Window is not displayed, select Properties Window from the View menu.

  5. Connect the Assign activity to the Prompt activity. To do this, hover the mouse over the Assign activity and click the rectangle at the bottom that appears when the mouse is over the activity. Drag the mouse down to the top of the Prompt activity. When the mouse is over the Prompt activity, four rectangles appear. Drag the mouse so that the connecting line from the bottom of the Assign activity connects to the top rectangle of the Prompt activity, and then release the mouse button.

  6. Drag an Assign activity from the Primitives section of the Toolbox and drop it so that it is below the Prompt activity.

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

  8. Follow the preceding steps to connect the Prompt activity to the Assign activity.

  9. Drag a FlowDecision from the Flowchart section of the Toolbox and drop it below the Assign activity. Connect the Assign activity to the FlowDecision activity, and then click the FlowDecision activity to select it. In the Properties Window, type the following expression into the Condition property value box.

    Guess = Target
    
  10. Drag another FlowDecision activity from the Toolbox and drop it below the first one. Connect the two activities by dragging from the rectangle that is labeled False on the top FlowDecision activity to the rectangle at the top of the second FlowDecision activity.

    Gg983474.Tip(en-us,VS.100).gifTip:
    If you do not see the True and False labels on the FlowDecision, hover the mouse over the FlowDecision.

  11. Click the second FlowDecision activity to select it. In the Properties Window, type the following expression into the Condition property value box.

    Guess < Target
    
  12. Drag two WriteLine activities from the Primitives section of the Toolbox and drop them so that they are side by side below the two FlowDecision activities. Connect the True action of the bottom FlowDecision activity to the leftmost WriteLine activity, and the False action to the rightmost WriteLine activity.

  13. Click the leftmost WriteLine activity to select it, and type the following expression into the Text property value box in the Properties Window.

    "Your guess is too low."
    
  14. Connect the WriteLine to the left side of the Prompt activity that is above it.

  15. Click the rightmost WriteLine activity to select it, and type the following expression into the Text property value box in the Properties Window.

    "Your guess is too high."
    
  16. Connect the WriteLine activity to the right side of the Prompt activity above it.

    The following example illustrates the completed workflow.

    Gg983474.937fc39a-cff4-487c-9fa3-35b0a7e06771(en-us,VS.100).gif

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

Tasks

How to: Create an Activity
How to: Run a Workflow

Reference

Flowchart
FlowDecision

Other Resources

Windows Workflow Foundation Programming
Designing Workflows
Getting Started Tutorial