Book Image

Microsoft Windows Workflow Foundation 4.0 Cookbook

By : Andrew Zhu
Book Image

Microsoft Windows Workflow Foundation 4.0 Cookbook

By: Andrew Zhu

Overview of this book

Windows Workflow Foundation 4.0 (WF) is a significant part of .NET Framework 4.0. WF makes workflow technology available to every single programmer that uses the .NET Framework 4.0. It is easy to create long running and distributed programs using WF with the right knowledge. With this book, you will discover that working with workflows is easy. This book provides both step-by-step recipes and relevant background information. It covers all the important aspects of Windows Workflow Foundation 4.0. The best thing about this book is that all recipes are based on real-world experiences of Andrew Zhu. Andrew is a global WF and BizTalk technology support engineer for Microsoft. This book covers everything you need to know, when working with workflows. Get to grips with flow control activities, messaging, and transaction processes with easy to understand steps followed by explanations. You will quickly learn to use collection and custom WF activities and WF services.You will see recipes that illustrate integration of Windows Workflow with other applications such as WPF, ASP.NET, WCF service.Lastly, you will discover how easily you can customize W4 Designer with WF rule engine and others.
Table of Contents (15 chapters)
Microsoft Windows Workflow Foundation 4.0 Cookbook
Credits
About the Author
About the Reviewers
Foreword
Preface
Index

Running a WF program asynchronously


In the previous tasks, we used the WorkflowInvoker.Invoke method to start a workflow instance on the same thread as the main program. It is easy to use; however, in most real applications, a workflow should run on an independent thread. In this task, we will use WorkflowApplication to run a workflow instance.

How to do it...

  1. Create a workflow project:

    Create a new Workflow Console Application under the Chapter01 solution and name the project as UseWorkflowApplication.

  2. Author a workflow:

    In the opening Workflow1.xaml designer, click on Arguments, create two Int32 InArguments for Number1 and Number2. Create an Int32 OutArgument for Result. Add an Assign activity to the workflow designer panel. In the Assign activity, type Result=Number1+Number2.

  3. Write code to host the workflow:

    Open Program.cs file and change code as follow:

    using System;
    using System.Activities;
    using System.Activities.Statements;
    using System.Threading;
    using System.Collections.Generic;
    
    namespace UseWorkflowApplication{
        class Program{
            static void Main(string[] args){
                AutoResetEvent syncEvent = 
                   new AutoResetEvent(false);
                IDictionary<string, object> input = 
                   new Dictionary<string, object>()
                {
                    {"Number1",123},
                    {"Number2",456}
                };
                IDictionary<string,object> output=null;
                WorkflowApplication wfApp = 
                   new WorkflowApplication(new Workflow1(),input);
                wfApp.Completed = 
                delegate(WorkflowApplicationCompletedEventArgs e)
                {
                    Console.WriteLine("Workflow thread id:"+
                            Thread.CurrentThread.ManagedThreadId);
                    output = e.Outputs;
                    syncEvent.Set();
                };
                wfApp.Run();
                syncEvent.WaitOne();
                Console.WriteLine(output["Result"].ToString());
                Console.WriteLine("Host thread  id:"+Thread.CurrentThread.ManagedThreadId);
            }
        }
    }
  4. Run it:

    Set UseWorkflowApplication as Startup project. Press Ctrl+F5 to build and run the workflow without debugging. The application should run in a console window and print the following message:

How it works...

The function of this workflow is adding two InArgument Numbers and assigning the result to an OutArgument Result.

AutoResetEvent syncEvent = new AutoResetEvent(false);

As the workflow thread runs simultaneously with the caller thread, the caller thread may terminate before the workflow thread. To prevent this unexpected program quit, we need to use AutoResetEvent to synchronize caller and workflow thread.

syncEvent.WaitOne();

The caller thread will wait there, until syncEvent is set.

wfApp.Completed = 
delegate(WorkflowApplicationCompletedEventArgs e)
{
   output = e.Outputs;
   syncEvent.Set();
};

When the workflow completes, syncEvent.Set() is invoked. After that, the caller can continue running to its end.

Another thing we should be aware of is how we get the result when the workflow ends. Unlike the WorkflowInvoker.Invoker method, in a WorkflowApplication-style caller, we get dictionary output from WorkflowApplicationCompletedEventArgs's Outputs property; see the preceding code snippet.