.NET Framework 4 Platform Update 1 State Machine Workflows
This topic applies to Windows Workflow Foundation 4 (WF4).
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.
State Machine Terminology
This section defines the state machine vocabulary used throughout this topic.
A description of an activity that is to be executed at a given moment.
A situation in which a state machine can execute some actions and/or waiting for some events to occur.
- Entry Action
An action executed when entering the state
- Exit Action
An action 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.
An event which may fire the transition.
A constraint which will fire the transition only when it is evaluated to true after the trigger occurs.
- Transition Action
An action which is executed when performing a certain transition.
- Conditional Transition
A transition with an explicit condition.
A transition which transits from a state to itself.
- Initial State
A state which represents the starting point of the state machine.
- Final State
A state which represents the completion of the state machine.
State Machine Concepts
State machine workflow provide a modeling style with which you can model your workflow in an event-driven manner. A state machine consists of a set of states. Each state can have a set of transitions which specify the execution logic between the states. There are several classes in the state machine runtime:
A StateMachine itself is an activity, which means it can be used any place in a workflow that an activity can be used. A state machine can have multiple states. It must have an initial state but can have zero or multiple final states. You can define variables in the scope of a state machine.
A State is not an activity and it can only be used inside a state machine. A state can have an entry/exit action which will be executed every time the workflow enters or exits the state. You can specify any activity as the entry/exit action, including another StateMachine activity. A state can have a set of transitions which specifies the possible destination states when some trigger occurs.
Initial/Final state represents the start/completion of the entire state machine accordingly. A state cannot be initial state and final state at the same time. Final state cannot have any entry/exit action or transitions.
A Transition is not an activity and it can only be used inside a state machine. A transition must have a state set as its target. It must have a trigger which is used to start the transition as well. You can specify any activity as the trigger. You can also specify an expression as the condition of a transition. In this case, the transition will be performed only when the trigger is completed and the condition is evaluated to be true. If not, the transition won’t be performed and the trigger will be rescheduled. A transition can contain a transition action which will be executed when the transition is occurs.
You can have multiple transitions share the same trigger. In this case, all the transitions must have conditions specified. The target state of a transition is not necessarily a different state, which means a state can have self-transition pointing to itself. In this case, the entry/exit action will be executed as usual.
For more information and sample code, see How to: Create a State Machine Workflow and Windows Workflow Foundation (WF4) - Introduction to State Machine Hands On Lab.