May 21, 2019
Hot Topics:

Get Ready for Windows Workflow Foundation

  • November 22, 2006
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Building a sequential workflow with parameters

The example in this section adds some logic to the sequential workflow to make a decision based on a particular property value. Take the following steps to modify the prior example within Visual Studio 2005:

  1. Add a property of bool type to the code-behind file. Have the default value for the property set to false. I chose to name the property IsTrue.
  2. Drag an IfElse activity to the top of the workflow designer.
  3. Drag the prior helloActivity into the left side of the IfElse branch.
  4. Add a terminate activity to the right branch of the IfElse activity.
  5. Use the smart tag on the ifElseBranchActivity1 to set a declarative rule condition. This will allow you to use the designer to set the logic condition to use. The expression you should use is this.IsTrue == true, which will base the condition on the IsTrue property of the sequential workflow.
  6. Set a breakpoint in the workflow designer on the IfElse activity.
  7. Build and execute your project in debug mode. You'll notice that the "Hello World!" will be skipped because your condition is set to false by default.

Figure 2 shows a screenshot of the workflow designer with the modifications.

Click here for a larger image.

Figure 2: Workflow Designer with Modifications

Here is a modified version of the Program.cs class:

using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Workflow.Runtime;using System.Workflow.Runtime.Hosting;namespace HelloWorldWorkflow{   class Program   {      static void Main(string[] args)      {         using(WorkflowRuntime workflowRuntime =               new WorkflowRuntime())         {            AutoResetEvent waitHandle = new AutoResetEvent(false);            workflowRuntime.WorkflowCompleted +=               delegate(object sender, WorkflowCompletedEventArgs e)               {waitHandle.Set();};            workflowRuntime.WorkflowTerminated +=               delegate(object sender, WorkflowTerminatedEventArgs e)            {               Console.WriteLine(e.Exception.Message);               waitHandle.Set();            };            // Create a dictionary to hold workflow parameter values            Dictionary<string, object> parameters =               new Dictionary<string, object>();            parameters.Add("IsTrue", true);            // Pass parameter values to the workflow as it is created            WorkflowInstance instance = workflowRuntime.CreateWorkflow(               typeof(HelloWorldWorkflow.HelloWorkflow), parameters);            instance.Start();            waitHandle.WaitOne();         }      }   }}

I added some specific code to allow for parameters to be passed in to the HelloWorkflow. I did this by creating an instance of the Dictionary<string, object> generic class and assigning a key and value pair to it for the IsTrue property value. You would add additional parameters to the instance of the Dictionary class for any other parameters you chose to add to your workflow. By toggling the value back and forth in code from true and false, the workflow will display or not display "Hello World!" accordingly. This demonstrates a programmatic way to control the display. You could just as easily read the value from a configuration file, seek input from another source such as a web service, or take input from a user interface.

Here is the code from the HelloWorld.xoml.cs code-behind file with the additional property:

using System;using System.ComponentModel;using System.ComponentModel.Design;using System.Collections;using System.Drawing;using System.Workflow.ComponentModel.Compiler;using System.Workflow.ComponentModel.Serialization;using System.Workflow.ComponentModel;using System.Workflow.ComponentModel.Design;using System.Workflow.Runtime;using System.Workflow.Activities;using System.Workflow.Activities.Rules;namespace HelloWorldWorkflow{   public partial class HelloWorkflow : SequentialWorkflowActivity   {      private bool isTrue = false;      public bool IsTrue      {         get { return this.isTrue; }         set { this.isTrue = value; }      }      private void helloActivity_EventHandler(object sender,                                              EventArgs e)      {         Console.WriteLine("Hello World!");      }   }}

WinWF and Sequential Workflow

Just like that, you've gotten a WinWF background, workflow basics, activity basics, authoring workflows, and some sequential workflow examples. Hopefully, you found the topic intriguing and are ready to start exploring WinWF within your applications.

Future Columns

The topic of the next column is yet to be determined. It likely will be either state machine workflow, building custom activities, or building a rules-based workflow using WinWF. However, if you have something in particular that you would like to see explained here, contact me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer (MCSD, MCSE, MCDBA) is a senior architect of .NET applications for large and mid-sized organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in the architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the second year in a row. You can reach Mark at mstrawmyer@crowechizek.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date