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.
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.
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.
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.
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.
Step 6 : Now we add Console Application.
- Go to Solution Explorer and right-click.
- Select Add->New Project.
- Select Console Application.
Step 7 : Right-click on Console
Application and select Set As Startup Project.
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.