Wednesday, November 09, 2011

Creating your First Windows Workflow v4 Application

The following WF console application (written in C#) will 1) pause for 5 seconds, 2) write the line "Hello," 3) pause another 5 seconds, and then 4) write the line "World!" I know, not terribly practical, but it will get you familiar with creating WF v4 applications for the first time, assuming you'r new to this, that is.
  1. Open up Visual Studio 2010.
  2. Press Ctrl-Shift-N to create a new project.
  3. In the 'Installed Templates' pane, select 'Workflow'.
  4. Select the 'Workflow Console Application' template.
  5. Name the project DotNetFun.Workflow.HelloWorld.
  6. Click on OK.
  7. Ensure that the 'Workflow1.xaml' file is open.
  8. Drag a 'Sequence' from the toolbox's 'Control Flow' section onto the area of the designer labeled 'Drop activity here'.
  9. Click on the sequence to select it.
  10. At the bottom of the designer, click on the 'Variable' tab button.
  11. Click on where it says 'Create Variable'.
  12. Type pauseWaitTime. This will be the variable that stores how long each pause will be.
  13. In the 'Variable type' column, select 'Browse for Types...', then in 'Type Name:' text box, type System.TimeSpan.
  14. Click on 'OK'.
  15. In the 'Default' column, type the following code expression: new TimeSpan(0,0,5)
    The 'pauseWaitTime' variable will now be available within the 'Sequence' scope, available to the sequence  and all of its child elements.
  16. In the 'Primitives' section of the toolbox, drag a 'Delay' activity into the 'Sequence' sequence.
  17. In the properties explorer for the Delay activity, enter pauseWaitTime in the 'Duration' text box.
  18. In the properties explorer for the Delay activity, change the name to Delay1.
  19. In the 'Primitives' section of the toolbox, drag a 'WriteLine' activity directly beneath the 'Delay1' activity.
  20. Click on the 'Enter a VB expression' text box and enter "Hello," (with double quotes, as this is a string literal expression).
  21. In the properties explorer for the WriteLine activity, change the name to WriteLine1.
  22. In the 'Primitives' section of the toolbox, drag a 'Delay' directly beneath the 'WriteLine1' activity.
  23. In the properties explorer for the Delay activity, enter pauseWaitTime in the 'Duration' text box.
  24. In the properties explorer for the Delay activity, change the name to Delay2.
  25. In the 'Primitives' section of the toolbox, drag a 'WriteLine' activity directly beneath the 'Delay2' activity.
  26. Click on the 'Enter a VB expression' text box and enter "World!" (with double quotes, as this is a string literal expression).
  27. In the properties explorer for the WriteLine activity, change the name to WriteLine2. The workflow designer should now look like the following:
  28. Open the Program.cs class file.
  29. Add the following code after the WorkflowInvoker.Invoke method call:

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    
    
    
    
  30. Press F5 to run and test the workflow application.
That does it for this very simple introduction to WF v4 workflows. Be on the lookout for more!

No comments: