Reader Level:
ARTICLE

Lifetime Events in Workflow

Posted by Davin Martyn Articles | Workflow Foundation in C# November 14, 2011
In this article we define Lifetime Events in Workflow. WorkflowRuntime exposes a number of events we can use to detect changes in a running workflow.
  • 0
  • 0
  • 3459

Introduction : Windows Workflow offers two workflow execution styles out of the box: sequential and event-driven. A sequential workflow completes one activity and moves to the next, executing a sequence of consecutive steps. Event-driven workflows model as a state machine. A state machine consists of a set of states (including an initial state and a final state), and a set of events. The state machine is always in one of the defined states, and cannot transition to a new state until an event arrives. Workflow Runtime exposes a number of events we can use to detect changes in a running workflow.

Following Events are given below :

  • WorkflowAborted.
  • WorkflowCreated.
  • WorkflowCompleted.
  • WorkflowStarted.

Step 1 : Open Visual Studio 2010.

  • Go to File->New->Project.
  • Select Sequential Workflow Library.
eve1.gif

Step 2 : Now drag activity from Toolbox.

  • Drag Sequence, Code Activity.
eve2.gif

Step 3 : Now go to Solution Explorer and right-click

  • Add -> New Item->Activity ( Code Separation).
  • When we click OK then the following design window open.
eve3.gif

eve4.gif

Step 4 : Now go to program.cs option and write the below code.

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;
namespace WorkflowConsoleApplication16
{
    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();
                };
                Console.WriteLine("Running Workflow Events :");
                Console.WriteLine("Workflow created");
                Console.WriteLine("Workflow started");
                Console.ReadLine();
                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication16.Workflow1));
                instance.Start();
                waitHandle.WaitOne();
            }
        }
    }
}

Step 5 : Now go to Activity1.xaml.cs file and define the event and code for the Sequential Workflow.

Code :

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
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 WorkflowConsoleApplication16
{
    public partial class Activity1 : SequenceActivity
    {
        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            Console.WriteLine("Executing...");
        }
        static void runtime_WorkflowIdled(object sender, WorkflowEventArgs e)
        {
            Console.WriteLine("Workflow idled");
        }
        static void runtime_WorkflowCreated(object sender,
        WorkflowEventArgs e)
        {
            Console.WriteLine("Workflow created");
        }
        static void runtime_WorkflowTerminated(object sender,WorkflowTerminatedEventArgs e)
        {
            Console.WriteLine("Workflow terminated");
            Console.WriteLine("\tException: " + e.Exception.Message);
            Console.ReadLine();
        }
    }
}

Step 6  : Now Build the application and check the error are present or not.

Step 7 : Now again go to Program.cs file and define the following code for the WorkflowRuntimeEvent.

Code :
            runtime.WorkflowCreated += new EventHandler<WorkflowEventArgs>(runtime_WorkflowCreated)
            runtime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(runtime_WorkflowIdled);
            AutoResetEvent waitHandle = new AutoResetEvent(false);

Step 8 : Press F5 and run the application.

eve5.gif

COMMENT USING