State Machine in Workflow

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 the 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 :
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Workflow.Activities;  
  6. namespace WorkflowLibrary1  
  7. {  
  8.     [ExternalDataExchange]  
  9.     public interface Interface1  
  10.     {  
  11.         event EventHandler<ExternalDataEventArgs> unlock;  
  12.         event EventHandler<ExternalDataEventArgs> inlock;  
  13.     }  
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 :
  1. using System;  
  2. using System.Linq;  
  3. using System.Activities;  
  4. using System.Activities.Statements;  
  5. namespace WorkflowLibrary1  
  6. {  
  7.   class Program  
  8.     {  
  9.        [Serializable]  
  10.     public class Locker :  
  11.     {  
  12.         public event EventHandler<ExternalDataEventArgs> Lock;  
  13.         public event EventHandler<ExternalDataEventArgs> Unlock;  
  14.         public bool lockerState = false;  
  15.    
  16.         public bool LockIt(Guid InstanceID)  
  17.         {  
  18.             if (this.Lock != null)  
  19.             {  
  20.                 this.Lock(this,new System.Workflow.Activities.ExternalDataEventArgs(InstanceID));  
  21.                  return true;  
  22.             }  
  23.             else  
  24.             {  
  25.                 return false;  
  26.             }  
  27.         }  
  28.         public bool UnlockIt(Guid InstanceID)  
  29.         {  
  30.             if (this.Unlock != null)  
  31.             {  
  32.                 this.Unlock(this,new System.Workflow.Activities.ExternalDataEventArgs(InstanceID));  
  33.                 return false;  
  34.             }  
  35.             else  
  36.             {  
  37.                 return true;  
  38.     }  
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.
    1. using System.Workflow.Activities;  
    2. using System.Workflow.ComponentModel;  
    3. using System.Workflow.Runtime; 
Step 9 : Now we go to Workflow1.cs [Design] option.
  • Double -click in Code Activity.
  • Write the below code.
Code :
  1. private void WriteState_ExecuteCode(object sender, EventArgs e) {  
  2.     Console.WriteLine("Locked!!!");  
Step 10: Go to the Program.cs file and write the below code.
 
Code :
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Workflow.Activities;  
  6. using System.Workflow.ComponentModel;  
  7. using System.Workflow.Runtime;  
  8. namespace ConsoleApplication1  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             WorkflowRuntime workflowruntime = new WorkflowRuntime();  
  15.             Locker locker = new Locker();  
  16.             Guid locker_ID = Guid.NewGuid();  
  17.             Dictionary<stringobject> Params = new Dictionary<stringobject>();  
  18.             ExternalDataExchangeService ExDateExchService = new ExternalDataExchangeService();  
  19.             workflowruntime.AddService(ExDateExchService);  
  20.             ExDateExchService.AddService(locker);  
  21.             WorkflowInstance wfinstance = workflowruntime.CreateWorkflow(typeof(Workflow1), Params, locker_ID);  
  22.             wfinstance.Start();  
  23.             locker.LockIt(locker_ID);  
  24.             Console.ReadLine();  
  25.             locker.UnlockIt(locker_ID);  
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
Step 11: Now Press F5 and run the application.
 
sm10.gif


Recommended Free Ebook
Similar Articles