Partilhar via


Flowchart Workflows

This topic applies to Windows Workflow Foundation 4 (WF4).

A flowchart is a well-known paradigm for designing programs. The Flowchart activity is typically used to implement non-sequential workflows, but can be used for sequential workflows if no FlowDecision nodes are used.

Flowchart Workflow Structure

A Flowchart activity is an activity that contains a collection of activities to be executed. Flowcharts also contain flow control elements such as FlowDecision and FlowSwitch that direct execution between contained activities based on the values of variables.

Types of Flow Nodes

Different types of elements are used depending on the type of flow control required when the element executes. Types of flowchart elements include:

  • FlowStep - Models one step of execution in the flowchart.

  • FlowDecision - Branches execution based on a Boolean condition, similar to If.

  • FlowSwitch – Branches execution based on an exclusive switch, similar to Switch.

Each link has an Action property that defines a ActivityAction that can be used to execute child activities, and one or more Next properties that define which element or elements to execute when the current element finishes execution.

Creating a Basic Activity Sequence with a FlowStep Node

To model a basic sequence in which two activities execute in turn, the FlowStep element is used. In the following example, two FlowStep elements are used to execute two activities in sequence.

<Flowchart>
  <FlowStep>    
  <Assign DisplayName="Get Name">
    <Assign.To>
      <OutArgument x:TypeArguments="x:String">[result]</OutArgument>
    </Assign.To>
    <Assign.Value>
      <InArgument x:TypeArguments="x:String">["User"]</InArgument>
    </Assign.Value>
  </Assign>
    <FlowStep.Next>
      <FlowStep>
        <WriteLine Text="[&quot;Hello, &quot; &amp; result]"/>
</FlowStep>
    </FlowStep.Next>
  </FlowStep>
</Flowchart>

Creating a Conditional Flowchart with a FlowDecision Node

To model a conditional flow node in a flowchart workflow (that is, to create a link that functions as a traditional flowchart's decision symbol), a FlowDecision node is used. The Condition property of the node is set to an expression that defines the condition, and the True and False properties are set to FlowNode instances to be executed if the expression evaluates to true or false. The following example shows how to define a workflow that uses a FlowDecision node.

<Flowchart>
  <FlowStep>
    <Read Result="[s]"/>
    <FlowStep.Next>
      <FlowDecision>
        <IsEmpty Input="[s]" />
        <FlowDecision.True>
    <FlowStep>
            <Write Text="Empty"/>
    </FlowStep>
        </FlowDecision.True>
        <FlowDecision.False>
    <FlowStep>
            <Write Text="Non-Empty"/>
          </FlowStep>
        </FlowDecision.False>
      </FlowDecision>
    </FlowStep.Next>
  </FlowStep>
</Flowchart>

Creating an Exclusive Switch with a FlowSwitch Node

To model a flowchart in which one exclusive path is selected based on a matching value, the FlowSwitch node is used. The Expression property is set to a Activity with a type parameter of Object that defines the value to match choices against. The Cases property defines a dictionary of keys and FlowNode objects to match against the conditional expression, and a set of FlowNode objects that define how execution should flow if the given case matches the conditional expression. The FlowSwitch also defines a Default property that defines how execution should flow if no cases match the condition expression. The following example demonstrates how to define a workflow that uses a FlowSwitch element.

<Flowchart>
    <FlowSwitch>
      <FlowStep x:Key="Red">
        <WriteRed/>
      </FlowStep>
      <FlowStep x:Key="Blue">
        <WriteBlue/>
      </FlowStep>
      <FlowStep x:Key="Green">
        <WriteGreen/>
      </FlowStep>
    </FlowSwitch>
</Flowchart>