This is part 2 in a multi-part series.  You can find part 1 here.

Last time, I walked through creating the "Hello World" equivalent of a state machine workflow.  Today, we will expand that workflow to contain additional states and events.  The goal will be to demonstrate how these concepts work in the state machine workflow.

As I mentioned before, state machine workflows are event driven.  Each workflow contains a collection of states.  Each state contains a collection of allowed events (called "event driven" activities).  Each event driven activity is, itself, a sequential workflow.  This sequential workflow describes the processing that occurs when the event is received.  Typically, but not always, it contains a "set state" activity, which changes the current state of the workflow.

A state machine workflow is always in exactly one state.  It starts in the initial state, and terminates when it reaches the completed state.  When an event is received, the workflow engine determines if that event is valid for the current state of the workflow.  If so, that event is delivered to the workflow and the corresponding event driven activities are executed.  This processing may change the current workflow state.  This continues until the current state is set to the completed state, at which point the workflow exits.

To see this in action, I expanded the HelloWorld workflow to contain three new states (Process, Review and Approval) and 3 new events (Process_Event, Review_Event and Approval_Event).  You can obtain the modifed code from here.

If you experiment with the workflow, you will notice a couple of things.  The same event is allowed in multiple states.  The workflow engine will deliver the event to the appropriate event driven activity, based upon the current state. If you send an event that is not available in the current state, the workflow engine will hold that event and then deliver it once the workflow enters an appropriate state.  Finally, if you send an event after the workflow has terminated, you will get an error message telling you that the workflow is no longer running.