Welcome to MSDN Blogs Sign in | Join | Help

SYSK 356: How To Execute A Workflow Synchronously Without waitHandle.WaitOne()

Consider the following code snippet frequently found in sample applications, code snippets and published articles:

 

using (WorkflowRuntime wr = new WorkflowRuntime("WorkflowRuntimeConfig"))

{

    wr.StartRuntime();

 

    using (System.Threading.AutoResetEvent waitHandle = new System.Threading.AutoResetEvent(false))

    {

        wr.WorkflowCompleted += delegate(object s, WorkflowCompletedEventArgs args) { waitHandle.Set(); };

        wr.WorkflowTerminated += delegate(object s, WorkflowTerminatedEventArgs args)

        {

            System.Diagnostics.Debug.WriteLine(args.Exception.ToString());

            waitHandle.Set();

        };

        wr.WorkflowAborted += delegate(object s, WorkflowEventArgs args) { waitHandle.Set(); };

 

 

        WorkflowInstance w = wr.CreateWorkflow(typeof(YourNamespace.YourWorkflow), parameters);

        w.Start();

 

        waitHandle.WaitOne();

    }

}

 

 

In essence, all that’s needed is to run a workflow synchronously…

 

Since you’re blocking the tread anyway, my recommendation is to simply run the workflow on the same thread.  It could be easily done by using the ManualWorkflowSchedulerService, transforming the code above into this:

 

 

using (WorkflowRuntime wr = new WorkflowRuntime("WorkflowRuntimeConfig"))

{                   

    wr.StartRuntime();

 

    ManualWorkflowSchedulerService scheduler = wr.GetService<ManualWorkflowSchedulerService>();

 

    WorkflowInstance w = wr.CreateWorkflow(typeof(YourNamespace.YourWorkflow), parameters);

    w.Start();

   

    scheduler.RunWorkflow(w.InstanceId);

}

 

And that’s all…

Published Friday, July 20, 2007 3:13 PM by irenak

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# SYSK 356: How To Execute A Workflow Synchronously Without waitHandle.WaitOne()

Friday, July 20, 2007 6:59 PM by Noticias externas

Consider the following code snippet frequently found in sample applications, code snippets and published

# re: SYSK 356: How To Execute A Workflow Synchronously Without waitHandle.WaitOne()

Saturday, July 21, 2007 4:01 AM by Anthony Tarlano

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker