In this installment we will look at some simple activities and their use. In the previous posts I have used the code activity so I will not repeat that and move on to other activities. We begin by creating a sequential workflow console project. The designer should come up for us to place activities.
Sequence Activity
The sequence activity allows you to create a block of activities just like you create a block of code statements by putting { } around them. As the name suggests the sequence actvitiy runs all the statements embedded in it in sequence. There are two types of activities in WF: basic and composite. The basic activity is the unit of task we perform, it performs one particular action. The composite activity on the other hand allows you to combine basic activities creating activity blocks. Sequence is one such activity.
We create handlers for them by double clicking.
and then run it see results.
If-Else Activity
The second activity that you need from your structured programming toolbox is the condition or if-else activity. To use this activity drop it from the toolbox on to the designer and move code activity one to its left brach which is represents the if condition and code 2 to the right branch which is else. Delete the sequence and it should look like this.
The red warning means we need to specify something. When you click on it says that the condition property is not set, so let's do that. There are two types of conditions in WF: code and declarative. Code condition is a function which evaluates to true/false and you can do any kind of complex processing in it. Declarative is simpler and is useful for simple expressions, its use is also more common. Let's define a code condition first. For that you need to write a function that looks something like this:
As you can see the evaluation is returned in the event parameter and you can do your complex evaluation before returning this result. Now go to the designer and change the condition property to Code Condition and then expand to select the handler.
once this is done run the program and it should execute code activity 1 since the evaluation is true. Change the evaluation and run again to see the other branch execute.
While Activity
The third structured programming activity is the loop, which is called While activity here. Drop a while activity from the toolbox to the designer window and move the if-else condition inside the loop, it should look like this.
The while also needs a condition, this time we will use the declarative condition. click on the red warning and select the condition. But first add a variable to the workflow class so that we can create condition on something.
Now go to condition and add the following:
We have put a condition on count but now we also need to add code to subtract the value for every iteration. For the moment lets add the statement "count--;" to both the handlers of code activity 1 and 2. Running the workflow now gives us:
Code activity 1 was executed ten times because the if condition is returning true every time. Lets change it to switch between the two code activities using the odd/even test. Change the condition for if-else to the following:
then run again.
Parallel and Delay Activity
Now let's use the parallel and delay. They are not linked in anyway but for the purpose of this post I am describing together. Drop a parallel activity from the toolbox to the designer in the else branch of the if-else. A parallel activity is the opposite of sequence and it executes activities in both branches independent of each other. Although the commonality between them is that both complete when all of their activities complete, so parallel would complete when all branches do. The designer should look like this:
Lets configure the delay activity now. Its purpose is just to create a delay of whatever time you want to delay your workflow. In this case we are delaying the second branch of the parallel activity, which means that the whole of parallel activity would be delayed by this time, as it waits for all of its branches to complete, unless the other branch takes more time. Go to the properties of the delay activity and assign a two second delay to it.
Now run the workflow. Every second iteration should take more time now.
This completes the third part of the series. In the next part we will look at the fault and cancellation handlers.