Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

Jump Start State Machine Workflow

Rate me:
Please Sign up or sign in to vote.
3.27/5 (23 votes)
5 Apr 20077 min read 122.2K   1.4K   53  
This is second in series of articles on Windows Workflow Foundation
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;

#endregion

namespace FirstWFStateMachine
{
    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();
                };

                ExternalDataExchangeService ExDateExchService = new ExternalDataExchangeService();
                workflowRuntime.AddService(ExDateExchService);

                POService poServiceInstance = new POService();
                ExDateExchService.AddService(poServiceInstance);

                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(FirstWFStateMachine.POWorkflow));
                instance.Start();

                
                PO newPO = new PO();
                Console.WriteLine("Please Enter Item Code to be sent Purchase Order of");
                string ItemCode = Console.ReadLine();
                Console.WriteLine("Please Enter Due Delivery Date of Purchase Order of");
                DateTime DueDeliveryDate = Convert.ToDateTime(Console.ReadLine());
                newPO.ItemCode = ItemCode;
                newPO.PODueDeliveryDate = DueDeliveryDate;

                poServiceInstance.CreatePO(instance.InstanceId, newPO);
                Console.WriteLine("New PO created for item: " + newPO.ItemCode + " with Due Delivery Date: " + newPO.PODueDeliveryDate.Date);
                
                

                Console.WriteLine("Do you want to approve Purchase Order");
                while ( (Console.ReadLine().ToLower()) != "y")
                {
                    Console.WriteLine("Current status of Purchase Order : Created");
                    Console.WriteLine("Waiting to be Authorised");
                    DumpStateMachine(workflowRuntime, instance.InstanceId);
                    Console.WriteLine("Do you want to approve Purchase Order");
                }
                poServiceInstance.AuthorisePO(instance.InstanceId, newPO);

                Console.WriteLine("Do you want to send Purchase Order");
                while ((Console.ReadLine().ToLower()) != "y")
                {
                    Console.WriteLine("Current status of Purchase Order : Created");
                    Console.WriteLine("Waiting to be Sent");
                    DumpStateMachine(workflowRuntime, instance.InstanceId);
                    Console.WriteLine("Do you want to send Purchase Order");
                }
                poServiceInstance.SendPO(instance.InstanceId, newPO);
                DumpStateMachine(workflowRuntime, instance.InstanceId);

                System.Console.ReadKey();
                waitHandle.WaitOne();

            }
        }

        private static void DumpStateMachine(WorkflowRuntime runtime, Guid instanceID)
        {
            StateMachineWorkflowInstance instance =
                   new StateMachineWorkflowInstance(runtime, instanceID);

            Console.WriteLine("Workflow ID: {0}", instanceID);
            Console.WriteLine("Current State: {0}",
                      instance.CurrentStateName);
            Console.WriteLine("Possible Transitions: {0}",
                      instance.PossibleStateTransitions.Count);
            foreach (string name in instance.PossibleStateTransitions)
            {
                Console.WriteLine("\t{0}", name);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) ITIM Systems
Pakistan Pakistan
Ali is Development Lead at ITIM Systems. He has got engineering degree in computer systems and likes to write about his experiences with Microsoft technologies. Ali's blog address is http://aliwriteshere.wordpress.com

Comments and Discussions