Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Windows Workflow Foundation - Sequential Workflow Example

3.59/5 (13 votes)
30 Sep 2007CPOL3 min read 1   1.3K  
An article describing how to program with Windows Workflow Foundation which comes with .NET Framework 3.0

Introduction

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:

Background

In order to create WF applications, you need to install some required packages/tools.
These are:

  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 that you 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 an 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, the 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 into 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.

Screenshot - 1.jpg

After creating the application, your designer window should look like this:

Screenshot - 2.jpg

NOTE: If you can't see Workflow1.cs[Design] or if there is something wrong with the 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 DelayActivitys into each IfElse branch. Finally, add one more CodeActivity after IfElseActivity. We'll configure these activities.

In Workflow1.cs class, add these private variables:

C#
private int i;
private DateTime start , finish; 

In design view, double-click codeActivity1. This will take you to the 'Code View' window. There, add the code snippet below:

C#
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
    i = (new Random()).Next(10);
    Console.WriteLine("Number = " + i);
    Console.WriteLine("Started...");
    start = DateTime.Now;
}

Go back to design view and click ifElseBranchActivity1. In the properties window, you will see the 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:

C#
this.i < 5 

Do the same for the other branch and now name it as GreaterThan and give the expression:

C#
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:

C#
private void codeActivity2_ExecuteCode(object sender, EventArgs e)
{
     Console.WriteLine("Finished...");
     finish = DateTime.Now;
     Console.WriteLine("Time Elapsed : " + finish.Subtract(start));
     Console.ReadLine();
}

After all these changes, your design view should look like this:

Screenshot - 4.jpg

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 shown below:

Screenshot - 5.jpg

Screenshot - 6.jpg

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.

History

  • 1st October, 2007: First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)