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.
sm1.gif
`sm.gif
Step 2: Add an Interface to declare the event.
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. }
  14. }
Step 3: Now we add a Class implementing this interface and we have the following methods to fire the Event.
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. public bool LockIt(Guid InstanceID)
  16. {
  17. if (this.Lock != null)
  18. {
  19. this.Lock(this,new System.Workflow.Activities.ExternalDataEventArgs(InstanceID));
  20. return true;
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. }
  27. public bool UnlockIt(Guid InstanceID)
  28. {
  29. if (this.Unlock != null)
  30. {
  31. this.Unlock(this,new System.Workflow.Activities.ExternalDataEventArgs(InstanceID));
  32. return false;
  33. }
  34. else
  35. {
  36. return true;
  37. }
  38. }
Step 4: Now go to Workflow1.cs [Design] option.
sm5.gif
Step 5: Now go to Workflow1.cs design option and select HandleExternalEvent and right-click.
sm6.gif
Step 6: Now we add 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.
Step 9 : Now we go to Workflow1.cs [Design] option.
Code :
  1. private void WriteState_ExecuteCode(object sender, EventArgs e) {
  2. Console.WriteLine("Locked!!!");
  3. }
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<string, object> Params = new Dictionary<string, object>();
  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. }
  29. }
Step 11: Now Press F5 and run the application.
sm10.gif