State Machine Workflows
A state machine is a well-known paradigm for developing programs. The StateMachine activity, along with State, Transition, and other activities can be used to build state machine workflow programs. This topic provides an overview of creating state machine workflows.
State machine workflows provide a modeling style with which you can model your workflow in an event-driven manner. A StateMachine activity contains the states and transitions that make up the logic of the state machine, and can be used anywhere an activity can be used. There are several classes in the state machine runtime:
To create a state machine workflow, states are added to a StateMachine activity, and transitions are used control the flow between states. The following screenshot, from the Getting Started Tutorial step How to: Create a State Machine Workflow, shows a state machine workflow with three states and three transitions. Initialize Target is the initial state and represents the first state in the workflow. This is designated by the line leading to it from the Start node. The final state in the workflow is named FinalState, and represents the point at which the workflow is completed.
A state machine workflow must have one and only one initial state, and at least one final state. Each state that is not a final state must have at least one transition. The following sections cover creating and configuring states and transitions.
A State represents a state in which a state machine can be in. To add a State to a workflow, drag the State activity designer from the State Machine section of the Toolbox and drop it onto a StateMachine activity on the Windows Workflow Designer surface.
To configure a state as the Initial State, right-click the state and select Set as Initial State. Additionally, if there is no current initial state, the initial state can be designated by dragging a line from the Start node at the top of the workflow to the desired state. When a StateMachine activity is dropped onto the workflow designer, it is pre-configured with an initial state named State1. A state machine workflow must have one and only one initial state.
A state that represents a terminating state in a state machine is called a final state. A final state is a state that has its IsFinal property set to
true, has no Exit activity, and no transitions originating from it. To add a final state to a workflow, drag a FinalState activity designer from the State Machine section of the Toolbox and drop it onto a StateMachine activity on the Windows Workflow Designer surface. A state machine workflow must have at least one final state.
A state can have an Entry and an Exit action. (A state configured as a final state may have only an entry action). When a workflow instance enters a state, any activities in the entry action execute. When the entry action is complete, the triggers for the state’s transitions are scheduled. When a transition to another state is confirmed, the activities in the exit action are executed, even if the state transitions back to the same state. After the exit action completes, the activities in the transition’s action execute, and then the new state is transitioned to, and its entry actions are scheduled.
When debugging a state machine workflow, breakpoints can be placed on the root state machine activity and states within the state machine workflow. Breakpoints may not be placed directly on the transitions, but they may be placed on any activities contained within the states and transitions.
All states must have at least one transition, except for a final state which may not have any transitions. Transitions may be added after a state is added to a state machine workflow, or they can be created as the state is dropped.
To add a State and create a transition in one step, drag a State activity from the State Machine section of the Toolbox and hover it over another state in the workflow designer. When the dragged State is over another State, four triangles will appear around the other State. If the State is dropped onto one of the four triangles, it is added to the state machine and a transition is created from the source State to the dropped destination State. For more information, see Transition Activity Designer.
To create a transition after a state is added, there are two options. The first option is to drag the state from the workflow designer surface and hover it over an existing state and drop it on one of the drop points. This is very similar to the method described in the previous section. You can also hover the mouse over the desired source state, and drag a line to the desired destination state.
A single state in a state machine can have up to 76 transitions created using the workflow designer. The limit on transitions for a state for workflows created outside the designer is limited only by system resources.
A transition may have a Trigger, a Condition, and an Action. A transition’s Trigger is scheduled when the transition’s source state’s Entry action is complete. Typically the Trigger is an activity that waits for some type of event to occur, but it can be any activity, or no activity at all. Once the Trigger activity is complete, the Condition, if present, is evaluated. If there is no Trigger activity then the Condition is immediately evaluated. If the condition evaluates to
false, the transition is cancelled, and the Trigger activity for all transitions from the state are rescheduled. If there are other transitions that share the same source state as the current transition, those Trigger actions are cancelled and rescheduled as well. If the Condition evaluates to
true, or there is no condition, then the Exit action of the source state is executed, and then the Action of the transition is executed. When the Action completes, control passes to the Target state
Transitions that share a common trigger are known as shared trigger transitions. Each transition in a group of shared trigger transitions has the same trigger, but a unique Condition and Action. To add additional actions to a transition and create a shared transition, click the circle that indicates the start of the desired transition and drag it to the desired state. The new transition will share a same trigger as the initial transition, but it will have a unique condition and action. Shared transitions can also be created from within the transition designer by clicking Add shared trigger transition at the bottom of the transition designer, and then selecting the desired target state from the Available states to connect drop-down.
Note that if the Condition of a transition evaluates to
For more information on creating state machine workflows, see How to: Create a State Machine Workflow, StateMachine Activity Designer, State Activity Designer, FinalState Activity Designer, and Transition Activity Designer.
This section defines the state machine vocabulary used throughout this topic.
The basic unit that composes a state machine. A state machine can be in one state at any particular time.
An activity executed when entering the state
An activity executed when exiting the state
A directed relationship between two states which represents the complete response of a state machine to an occurrence of an event of a particular type.
A transition that shares a source state and trigger with one or more transitions, but has a unique condition and action.
A triggering activity that causes a transition to occur.
A constraint which must evaluate to
true after the trigger occurs in order for the transition to complete.
An activity which is executed when performing a certain transition.
A transition with an explicit condition.
A transition which transits from a state to itself.
A state which represents the starting point of the state machine.
A state which represents the completion of the state machine.