Create Simple Workflow
Create Simple Workflow
By : Kasim Wirama, MCDBA, MVP SQL Server
Workflow is series of activities that are linked one another. You can see many samples about workflow. For me, workflow looks like procedures that I should follow from beginning until the end. There is 2 kinds of workflow, sequential workflow and state machine workflow.
Sequential workflow is workflow based on step completion, and tends to move forward to the end of workflow, whereas state machine workflow is workflow based on state triggered to move from one state to another state, it can move backwards or forwards, and there are one or more state that completes the workflow.
If you run Visual Studio 2005, you need to install Windows Workflow Foundation (WWF) extension as add-on and .NET framework 3.0, or it is already bundled in Visual Studio 2008.
I will give you simple example how easy to create workflow. I create this sample in Visual Studio 2008. first create workflow project type, there are several kinds of WWF project, choose Sequential Workflow Console Application, name it SimpleWorkflow, and click OK.
By default, workflow projects include System.Workflow.Activities, System.Workflow.ComponentModel, System.Workflow.Runtime, and System.WorkflowServices. The project has also included Program.cs file and Workflow1.cs, instance of Workflow1.cs class will be called in main method of Program.cs.
I rename Workflow1.cs to TestWorkflow1.cs, double click and you will have workflow designer, drag CodeActivity from WindowsWorkflow v3.0 into designer, double click on the component, visual studio will add event handler of the component, this event of the component is ExecuteCode, it will get triggered when the workflow is invoked from workflow client (main method of Program.cs class), type inside the handler this code below.
Console.WriteLine (“message from workflow”);
Now you need to change a bit workflow invocation in workflow client (main method of Program.cs) with this code below :
static AutoResetEvent waitHandle = new AutoResetEvent(false); static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted); workflowRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(simpleWorkflow.testWorkflow)); instance.Start(); waitHandle.WaitOne(); Console.ReadLine(); } } static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { Console.WriteLine("workflow completed"); waitHandle.Set(); } static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception + " on workflow instance : " + e.WorkflowInstance.InstanceId.ToString()); waitHandle.Set();
}
Let’s go through the code one by one from main method as entry point of execution.
workflowRuntime object is created, it will host my workflow. My workflow is instantiated by WorkflowInstance. WorkflowRuntime object has several event, mainly when workflow is completed without error (i.e.: Workflow completed), and workflow is completed with error (i.e.:Workflow terminated). Then the workflow instance is started, when it is started, another thread is spawn, so thread execution is executed separately from main thread, and give opportunity for another workflow thread to run. Each method, I issue waithandle.Set(), means that I signal to main thread that spawned thread (for the workflow) has finished, and control execution is handed over to main thread to continue the execution of main thread.
I move AutoResetEvent object from main method to member of Program.cs class. The code waitHandle.WaitOne(); in main method means that main method is waiting signal from workflow thread, then continue its execution. I add Console.ReadLine(); so that I can see the message on the screen, otherwise my console window is close immediately after waitHandle.WaitOne(); is invoked by workflow thread.
Run it, and that’s it! You will begin to build workflow with Windows Workflow Foundation (WWF).