This article describes how to create and run a Sequential Workflow in Windows Workflow Foundation (formerly WWF, or just WF) which comes bundled with .NET Framework 3.0 and above.
Before we get started, if you are new to WF and .NET Framework 3.0, I think you'd better take a look at some articles about WF.Here are some useful links:
In order to create WF applications,you need to install some required packages/tool.
1-.NET Framework 3.0 Runtime
2-Visual Studio 2005 Professional Edition
3-Windows Workflow Foundation extensions for Visual Studio 2005
4-Windows Communication Foundation (WCF,formerly Indigo) & Windows Presentation Foundation (WPF,formerly Avalon)
The fourth one is optional in fact. However, there may be some troubles and you may get some weird error if you don't have WCF and WPF installed on your development computer. So, I suggest you to have WCF and WPF installed too.
Sequential workflows are workflows which run in an order. That means, everything happens after another certain thing happens. There is a defined road-map, and application follows it.
Now, I want to demonstrate an example. Assume that you want to delay 5 or 10 seconds and delay time depends on an integer generated randomly. Here, condition is very simple but in real life examples, even the most complex conditions are defined in "if" blocks. So, we will define the conditions in "if" blocks. If the number is less than 5, we will wait 5 seconds, else if the number is greater than 5 seconds we will wait 10 seconds.
Yes, I know that this sounds silly but this is a beginner example to WWFs and after you get familiar with WWFs, you can create your own more complex applications using more complex conditions.
Using the code
Let's dig in the code.
First we open .NET Visual Studio 2005. In the 'Start Page' window, click 'Project' from 'Create' row. In the 'New Project' window, extend 'Visual C#' node, then select 'Workflow'. There are many applications but we will create a state machine application so we select 'Sequential Workflow Console Application' and name it 'MySequentialWorkflow'. Then click 'OK'.
After creating the application, your designer window should look like this:
NOTE:If you can't see 'Workflow1.cs[Design]' or if there is something wrong with steps above, go check the requirements. Probably you missed some of them or they are not installed properly.
Now, add a 'CodeActivity' from toolbox to 'Workflow1'. Then an 'IfElseActivity' and two 'DelayActivity's into each IfElse branch. Finally, add one more 'CodeActivity' after 'IfElseActivity'. We'll configure these activities.
In 'Workflow1.cs' class, add these private variables:
private int i;
private DateTime start , finish;
In design view, double-click 'codeActivity1'. This will take you to 'Code View' window. There, add the code snippet below:
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
i = (new Random()).Next(10);
Console.WriteLine("Number = " + i);
start = DateTime.Now;
Go back to design view and click 'ifElseBranchActivity1'. In properties window, you will see 'Condition' row. Click it and select 'Declarative Rule Condition'. Now, expand the row and set 'Condition Name' as 'LessThan'. Click 'Expression' and in the 'Rule Condition Editor' window, write the code below:
this.i <5 and="" br="" expression:="" give="" reaterthan="" the="">
this.i >= 5
Click 'delayActivity1' and set 'TimeoutDuration' as 00:00:05. Do the same for 'delayActivity2' and set it as 00:00:10.
Last thing, double click 'codeActivity2' and add the code snippet below:
private void codeActivity2_ExecuteCode(object sender, EventArgs e)
finish = DateTime.Now;
Console.WriteLine("Time Elapsed : " + finish.Subtract(start));
}After all these changes, your desing view should look like this:
Now, press 'F5' or click 'Run' in order to run the project. You will get different outputs but two possible outputs for value 4 and 8 are here:
Points of Interest
As I mentioned before, WF itself is not enough to create WF applications. You may have WCF and WPF installed as well as WF.