State Machine Workflow with WinWF
- 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; }}
private void codeActivityUserChoseBlue_Execute(object sender, EventArgs e){ Console.WriteLine("You chose blue!"); TryAgain = AskToTryAgain();}
Console.WriteLine("Workflow finished. Press enter to end.");Console.ReadLine();
Summary
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
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