October 19, 2019
Hot Topics:

State Machine Workflow with WinWF

  • April 10, 2007
  • By Mark Strawmyer, Rachel Wireman
  • Send Email »
  • More Articles »
  • Add a code activity named codeActivityUserChoseRed and set its ExecuteCode activity to codeActivityUserChoseRed_Execute. Visual Studio will switch to the handler in code behind. Add the following code to the handler:
  • private void codeActivityUserChoseRed_Execute(object sender,                                              EventArgs e){   Console.WriteLine("You chose red!");   TryAgain = AskToTryAgain();}private bool tryAgain = false;public bool TryAgain{   get { return tryAgain; }   set { tryAgain = value; }}private bool AskToTryAgain(){   TryAgain = false;   Console.Write("Would you like to try again (y or n)?  ");   string answer = Console.ReadLine();   while (!answer.Equals("y") && !answer.Equals("n"))   {      Console.Write("Please enter y or n: ");      answer = Console.ReadLine();   }   if (answer.Equals("y"))   {      return true;   }   else   {      return false;   }}
  • You will use a similar action for the blue state, so you can go ahead and add the following handler to the code behind for the blue state:
  • private void codeActivityUserChoseBlue_Execute(object sender,                                               EventArgs e){   Console.WriteLine("You chose blue!");   TryAgain = AskToTryAgain();}
  • Switch back to the designer for the red state. Add an IfElse activity below the code activity and name its left branch ifElseBranchActivityTryAgain.
  • Set the Condition property to Declarative Rule, expand the property, and click on the ellipsis. Create a new rule named ConditionTryAgain with a body of this.TryAgain and click OK to set it as the condition for this branch.
  • Add a SetState activity named setStateActivityTryAgain, and set TargetStateName to stateActivityStart. This will cause the workflow to cycle to the beginning state if the user chooses to make his/her favorite color choice again.
  • Name the right branch of the IfElse ifElseBranchActivityEnd. Leave its condition blank causing it to be an else branch. Add a SetState activity named setStateActivityEnd with a TargetStateName of stateActivityEnd. This will cause the workflow to end if the user chooses not to make his/her favorite color choice again.
  • Do the same thing you did for the red state now for the blue state. Be sure to set the execute code property of the blue state Code activity to the codeActivityUserChoseBlue_Execute method you prepared in advance earlier. The stateActivityBlue diagram should look similar to the following:
  • When you compile and run the example, it will run through and flash on the screen. You may want to add the following lines to the end Main in Program.cs to cause the application to pause at the end:
  • Console.WriteLine("Workflow finished.  Press enter to end.");Console.ReadLine();
  • You now should run the state machine workflow and watch it cycle through the different states as you make different choices. I used break points within Visual Studio to be able to step through the logic as it cycles.


This article covered state machine workflow with Windows Workflow Foundation. Even a simple example can turn lengthy in steps, but hopefully this look at state machine gave you an idea of the power and how it can be used to more closely match business and other processes. Much of the work in this example was done through the designer and configuring and setting properties.

Future Columns

The topic of the next column is yet to be determined. It will likely cover either building custom activities, building a rules-based workflow, or using the StateMachineTracking service to track state in state machine workflows. However, if you have something in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com.

About the Authors

Mark Strawmyer, MCSD, MCSE, MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in the architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fourth year in a row. You can reach Mark at mstrawmyer@crowechizek.com.

Rachel Wireman is a developer of applications for large and mid-size organizations. She specializes in development of Smart Client applications. Rachel is with Crowe Chizek in Oak Brook, Illinois and contributed to the examples within the article. You can reach Rachel at rwireman@crowechizek.com.

Page 2 of 2

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date