Working with State Workflows

State workflows are very useful when it comes to modeling human interaction workflows. But having a workflow to handle the business end is just one part of the equation, the user interface is another. Most of the time this user interface will need to have buttons and/or menu options to perform one of the possible actions. And how do you know which actions are possible in a given state? Well that is where the StateMachineWorkflowInstance class comes into play. Unfortunately this class is a little harder to find than the better known WorkflowInstance class. Why, well the WorkflowRuntime has a function GetWorkflow() taking a workflowId parameter to return a WorkflowInstance type but there is no matching function to return a StateMachineWorkflowInstance object. In fact you need to new up the StateMachineWorkflowInstance object passing in the WorkflowRuntime and the workflowId.

StateMachineWorkflowInstance stateMachine = _

new StateMachineWorkflowInstance(runtime, instanceId);


foreach (string possibleState in stateMachine.PossibleStateTransitions)

Console.WriteLine(“We can transition to {0}”, possibleState);


Among the more useful features of the StateMachineWorkflowInstance is the possibility to determine the current state, use the CurrentStateName or CurrentState properties, and the ability to determine all possible state transitions from the current state using the PossibleStateTransitions collections. This last collection tells you all the names of states that you can switch to from the current state. That is according to the state workflow as designed. Actually you can force a state switch to any other state using the SetState() function if you really want to but I guess that is a power feature that you should only use sparingly as it might be very confusing. So you need to know all the states in the StateWorkflow? Use the States collection and you are all done.



If you are using one or more HandleExternalEventActivity objects to force the state changes there are even more goodies to be found. This time not specific to state workflows as the regular WorkflowInstance object is the provider, use the WorkflowInstance property if you already have a reference to a StateMachineWorkflowInstance object. This time you want to take a good look at the GetWorkflowQueueData() function. This returns a collection of WorkflowQueueInfo objects. One of the properties these WorkflowQueueInfo objects expose if the IComparable QueueName. No big deal you might think as this queue name is usually a string or a Guid. Not in this case, now we get a an EventQueueName object back describing the interface and the event configured for the HandleExternalEventActivity we are using.

foreach (WorkflowQueueInfo queueInfo in _



EventQueueName eventQueue = queueInfo.QueueName as EventQueueName;

if (eventQueue != null)

Console.WriteLine(“Interface: {0} and method: {1}”, _

        eventQueue.InterfaceType.FullName, _





Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>