Search

Search:

Namespace:

Search Result
.
        • SetStateActivity
.

In figure 16, we've double-clicked on an Event Driven activity in OpenState to configure an event handler for the BugAssigned event. The event is part of a communication interface we've built with the ExternalDataExchange attribute, just as we did earlier in the section covering the Handle External Event activity. Notice the last activity inside the sequence is a SetState activity, which we cover next.

.

SetStateActivity

.

The SetState activity transitions a state machine to a new state. In figure 16, we are handling the BugAssigned event. When a bug is assigned, we want to transition to the AssignedState, so we set the TargetStateName property to AssignedState. The AssignedState activity will then have it's own set of Event Driven activities, each with SetState activities to transition to other states (and hopefully one day reach the ClosedState).

.

Use the TargetStateName just as in the standard SetStateActivity to indicate the target state.

.

Public Class DynamicSetStateActivity

.
    Public Shared TargetStateNameProperty As DependencyProperty = DependencyProperty.Register("TargetStateName", GetType(String), GetType(DynamicSetStateActivity))
.
            Return (CType((MyBase.GetValue(DynamicSetStateActivity.TargetStateNameProperty)), String))
.
            MyBase.SetValue(DynamicSetStateActivity.TargetStateNameProperty, Value)
.
        Dim queue As WorkflowQueue = wqs.GetWorkflowQueue("SetStateQueue")
.
        Dim e As New SetStateEventArgs(TargetStateName)
.

In figure 16, we've double-clicked on an Event Driven activity in OpenState to configure an event handler for the BugAssigned event. The event is part of a communication interface we've built with the ExternalDataExchange attribute, just as we did earlier in the section covering the Handle External Event activity. Notice the last activity inside the sequence is a SetState activity, which we cover next.

.

SetStateActivity

.

The SetState activity transitions a state machine to a new state. In figure 16, we are handling the BugAssigned event. When a bug is assigned, we want to transition to the AssignedState, so we set the TargetStateName property to AssignedState. The AssignedState activity will then have it's own set of Event Driven activities, each with SetState activities to transition to other states (and hopefully one day reach the ClosedState).

.

Note that the SetState function allows other state switches as well.

.

The StateWorkflow is the more fluid of the two. Basically the workflow is in a given state and waiting for events to move it into another state. This new target state can be every other state in the workflow. Normally this happens because an event is raised and the current state, or one of its parents knows how to handle the event and does so to trigger a state change. The workflow itself actually has another way, the SetStateQueue queue with a SetStateEventArgs argument to indicate the target state, which can be used to activate any other state, even if there is no event that has the new state as target. This makes state workflows very much like real live. After all how often are we not faced with issues that are not supposed to happen in the normal flow of things?

.

The SetStateActivity can be used in state machine workflows only.

.

The SetStateActivity only provides transitions to leaf StateActivity activities (a StateActivity that does not contain other StateActivity activities).

.

The SetStateActivity only provides transitions between two StateActivity classes and does not support StateActivity to parent StateActivity transitions or any other combination.

.

The SetStateActivity has to be a leaf node within the activity tree of an event handler.

.

There can be more than one SetStateActivity but all the activities must be leaf nodes.

.

SetStateActivity is always the last activity in the run path of a EventDrivenActivity activity.

.

The TargetStateName property of a SetStateActivity cannot be changed at runtime. Use the DynamicSetStateActivity if that kind of behavior is needed.

.

When working with state machine workflow's the SetStateActivity is one of the more important activities to use. The main reason is that is the one used to transition from one state to another. You can actually transition state in a different way as well using the StateMachineWorkflowInstance.SetState() function but the SetStateActivity is by far the most often used.

.

One of the cool features of the SetStateActivity that may not be immediately obvious is the fact that it doesn't do the state switch immediately. Instead it saved the new target start in an internal property, named NextStateName on the StateMachineExecutionState type to be exact, and only does the state switch when the current EventDrivenActivity or StateInitializationActivity is finished. And just to be clear those are the only two activities where you can use a SetStateActivity.

.

.

So the cool part is that you don't need to have the SetStateActivity right at the bottom of the EventDrivenActivity but it can be anywhere in the execution flow. Something that might make the execution logic quite a bit easier.

.

.

And the second cool behavior is that you can actually execute multiple SetStateActivity objects with different TargetStateName definitions and only the last one will be executed. Again something that might make the logic quite a bit simple as you can set a default TargetStateName at the beginning of your and make changes to the target as exceptional cases arise.

.

Both of these behaviors are especially important as the TargetStateName is a meta property, meaning it cannot be changed at runtime but only at design time. So the option of dynamically changing the SetStateActivity doesn't really exist.

.

The StateActivity represents a State in a StateMachineWorkflowActivity. State activities could contain one or more EventDrivenActivity, or StateActivity classes, one StateInitializationActivity class, and one StateFinalizationActivity class. SetState activities are used inside an EventHandlersActivity and StateInitializationActivity to move the state of the workflow.

.

Amongst the more useful functions is the SetState function that can be used to force the workflow into a specific state using the qualified name of the StateActivity. This is a wrapper round the SetStateQueue WorkflowQueue and the SetStateEventArgs class.

.

A StateWorkflow is one of the two basic workflow models included with WindowsWorkflowFoundation, the other being a SequentialWorkflow. A StateWorkflow is basically an implementation of a FiniteStateMachine. The main advantage of a StateWorkflow is that the workflow can switch between states at will, either because of a SetStateActivity in an event handler or explicitly by using the SetStateQueue queue with a SetStateEventArgs argument indicating the target state.