State Machine Workflow – Part 2: States and Events

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.


Comments (4)

  1. Wolfram says:

    Hi nice Article, you ever tried that with Sql Persistent Service ?

    If yes and you got it run may you can inform me!

  2. John Portnov says:

    Can you provide an ASP.NET with State Workflow designed as XOML and loaded as XMLReader (via CreateWorkflow)?

  3. This is part 3 in a multi-part series. You can find the previous entries here (part1, part2).

    Up to…

  4. Venkatesh says:


    I have created the sample state machine workflow in by VB code, with two states. the first state has state initialization and event driven activity, and the second state has state initialization. when start the workflow, it works fine at the initial state. when i raise a event using the handle external event activity it doesn’t move to next state.

    I know that i have made a mistake, but couldn’t able to trace. could you help me.

    here is the code

    <ExternalDataExchange()> _

    Public Interface ICodeset

       Event Add As EventHandler(Of ExternalDataEventArgs)

       Sub PageGoTo(ByVal args As CodesetEventArgs)

    End Interface

    <Serializable()> _

    Public Class Codeset

       Implements ICodeset

    Public Event Add(ByVal sender As Object, ByVal e As System.Workflow.Activities.ExternalDataEventArgs) Implements ICodeset.Add

    Public Sub OnAdd(ByVal instanceId As Guid)

            Dim args As New ExternalDataEventArgs(instanceId)

           RaiseEvent Add(Nothing, args)

    End Sub

    End Class

       Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

           Dim instance As WorkflowInstance

           instance = workflowRuntime.CreateWorkflow(GetType(WorkflowLibrary.Codeset))


           Session("InstanceID") = instance.InstanceId


           Dim str As String = instance.InstanceId.ToString


           objCodeset = New DataAccessLayer.Codeset



       End Sub

    Thanks in advance