In part 1of the getting started with WF guide we created our first flow which was a simple hello world example, in part 2 we will expand on this example and introduce the “do while” WF activity along with our first introduction to WF variables.
Step 1 – Recap from part 1
In part 1 we created our first WF flow we will now build on this to add in a loop which will write out to a console windows each time it iterates. Our design canvas after part 1 should look like the below screenshot with a sequence and a write line activity.
Step 2 – Adding the DoWhile activity
Under the “Control Flow” category in the toolbox drag a DoWhile activity into the sequence activity on the design service.
Next drag the existing WriteLine activity into the body section of the DoWhile
You will notice that we have warning message appear signified by the blue diamond and exclamation mark, this is just to make us aware that we haven’t added a condition to the DoWhile activity yet.
Before we can add the condition we first need to add a variable that will hold our loop count so that we are able to exit the DoWhile, we do this by selecting the DoWhile activity and then clicking on the Variables tab button in the bottom of the design service.
When the variables window expands up create a new variable called iLoopCount and give it a variable type of int32, you will note that the scope for the variable has been set to DoWhile this is because we selected that activity before creating the variable, we will cover variables and variable scope later in this series. Lastly just give the variable a default value of zero, so that once initialized we know that the value will be set.
Next under the primitive category drag an “Assign” activity into the DoWhile body just above the WriteLine activity. You will note that in WF4.5 visual studio will automatically add a sequence activity so that the Assign and WriteLine activity are contained within the same body.
In the Assign activity in the assignment field (left hand box) enter our variable name iLoopCount and in the assigned field (right hand box) add the calculation to add increment the variable by one by setting it to iLoopCount + 1.
Now in the condition section set the condition so that we exit the loop after five iterations by using the condition iLoopCound <=5, your designer should now be similar to the below.
Step 3 – Changing the WriteLine activity
One of the last things we need to change is the WriteLine activity so that it displays a more meaningful message, to make life a little easier we can use the properties window to create the expressions. To do this select the WriteLine activity and then go to the properties tab and click on the ellipsis button to open up the expression editor.
Delete the “Hello World” text out of the expression editor and replace it with “Iteration Num:” and then append the loop count to the message as in the below screenshot.
Your WF design service should now look similar to the below.
Step 4 – Execute our WF flow
Because we carried on and used the same WF flow from part 1 all the plumbing work to start up the flow is done for us already, so to execute its just a case of running the application and with any luck your output should be similar to the below.
Why not practise with changing the message text and the DoWhile condition to become more familiar with these WF activities.