SIGN UP MEMBER LOGIN:    
ARTICLE

State Machine in Workflow

Posted by Davin Martyn Articles | .NET 4.5 November 06, 2011
This article describes how to create and run a State Machine Workflow in Windows Workflow Foundation.
Reader Level:

Introduction : State-Machine workflow is an event driven workflow. The State machine workflow relies on external events to drive the workflow to completion. The workflow is always in one of the states, and has to wait for an event to arrive before transitioning to a new state workflow. The state machine defines a structure to follow.

Step 1 : Open Visual Studio.

  • Select File-> New-> Project.
  • Select State Machine Workflow Library.
sm1.gif

`sm.gif

Step 2 : Add an Interface to declare the event.

  • Go to Solution Explorer and right-click.
  • Add-> New Item.
  • Select Interface.
  • Write the below code.
sm2`.gif

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities;
namespace WorkflowLibrary1
{
    [ExternalDataExchange]
    public interface Interface1
    {
        event EventHandler<ExternalDataEventArgs> unlock;
        event EventHandler<ExternalDataEventArgs> inlock;
    }
}

Step 3 :
Now we add a Class implementing this interface and we have the following methods to fire the Event.

  • Go to Solution Explorer and right-click.
  • Add->New Item
  • Select Class.
  • Write the below code.
sm2.gif

Code :

using
System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
namespace WorkflowLibrary1
{
  class Program
    {
       [Serializable]
    public class Locker :
    {
        public event EventHandler<ExternalDataEventArgs> Lock;
        public event EventHandler<ExternalDataEventArgs> Unlock;
        public bool lockerState = false;
 
        public bool LockIt(Guid InstanceID)
        {
            if (this.Lock != null)
            {
                this.Lock(this,new System.Workflow.Activities.ExternalDataEventArgs(InstanceID));
                 return true;
            }
            else
            {
                return false;
            }
        }
        public bool UnlockIt(Guid InstanceID)
        {
            if (this.Unlock != null)
            {
                this.Unlock(this,new System.Workflow.Activities.ExternalDataEventArgs(InstanceID));
                return false;
            }
            else
            {
                return true;
    }
}

Step 4 : Now go to Workflow1.cs [Design] option.

  • Drag and Drop activity from Toolbox.
  • Drag Event Driven activity, HandleExternalEvent Activity, Code Activity.
sm5.gif

Step 5 : Now go to Workflow1.cs design option and select HandleExternalEvent and right-click.

  • Select Properties and give Event Name. Interface Type.
  • Give Interface1, Inlock.
sm6.gif

Step 6 : Now we add Console Application.

  • Go to Solution Explorer and right-click.
  • Select Add->New Project.
  • Select Console Application.
sm7.gif

Step 7 : Right-click on Console Application and select Set As Startup Project.

sm8.gif

Step 8 : Now we add reference to use Workflow application methods.

  • Select Console Application->Reference->Add Reference->.Net.

    using
    System.Workflow.Activities;
    using System.Workflow.ComponentModel;
    using System.Workflow.Runtime;

Step 9 : Now we go to Workflow1.cs [Design] option.

  • Double -click in Code Activity.
  • Write the below code.

Code :

private void WriteState_ExecuteCode(object sender, EventArgs e)
           {
                Console.WriteLine("Locked!!!");

               } 

Step 10 :
Go to the Program.cs file and write the below code.

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkflowRuntime workflowruntime = new WorkflowRuntime();
            Locker locker = new Locker();
            Guid locker_ID = Guid.NewGuid();
            Dictionary<string, object> Params = new Dictionary<string, object>();
            ExternalDataExchangeService ExDateExchService = new ExternalDataExchangeService();
            workflowruntime.AddService(ExDateExchService);
            ExDateExchService.AddService(locker);
            WorkflowInstance wfinstance = workflowruntime.CreateWorkflow(typeof(Workflow1), Params, locker_ID);
            wfinstance.Start();
            locker.LockIt(locker_ID);
            Console.ReadLine();
            locker.UnlockIt(locker_ID);
            Console.ReadLine();
        }
    }
}

Step 11 : Now Press F5 and run the application.

sm10.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor