Click here to Skip to main content
15,885,951 members
Articles / Programming Languages / C#

Running a Workflow Asynchronously To Improve The Performance

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
8 Mar 2012CPOL4 min read 40.9K   769   17  
In this article a simple math calculation is implemented in WF4.0 using different techniques to compare their execution time.
using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Diagnostics;
using System.Collections.Generic;
using System.Data;
using System.Threading;

namespace ParallelWorkflow
{
    class Program
    {
        static int numberOfCalcs = 100;
        static int numberOfThreadsRunning = numberOfCalcs;
        static string workflowInArg = "itemList";
        static ManualResetEvent eventDone = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            Trace.WriteLine(string.Format("Number Of Threads: {0}", numberOfCalcs));
            List<Double> items = new List<Double>();

            for (int i = 0; i < numberOfCalcs; i++)
                items.Add(System.Convert.ToDouble(i));

            Dictionary<string, object> p = new Dictionary<string, object>();
            p.Add(workflowInArg, items);

            //Linear Foreach
            RunSimpleForeach(p);

            //Parallel Foreach
            RunParallelForeach(p);

            //MultiThreading
            RunMultiThreading();

            Trace.Flush();
        }

        private static void RunMultiThreading()
        {
            Stopwatch sw = Stopwatch.StartNew();
            for (int workflowIndex = 0; workflowIndex < numberOfCalcs; workflowIndex++)
            {
                WorkflowApplication workflowApp = new WorkflowApplication(new MultiThreadWorkflow());
                workflowApp.Completed = WorkflowAppCompleted;
                workflowApp.Run();
            }

            eventDone.WaitOne();
            Trace.WriteLine("**** MultiThread Workflow, Elapsed Time: " + sw.Elapsed.ToString());
        }

        private static void RunParallelForeach(Dictionary<string, object> p)
        {
            Stopwatch sw = Stopwatch.StartNew();
            WorkflowInvoker.Invoke(new ParallelForeachWorkflow(), p);
            Trace.WriteLine("**** Parallel Foreach, Elapsed Time: " + sw.Elapsed.ToString());
        }

        private static void RunSimpleForeach(Dictionary<string, object> p)
        {
            Stopwatch sw = Stopwatch.StartNew();
            WorkflowInvoker.Invoke(new ForeachWorkflow(), p);
            Trace.WriteLine("**** Simple Foreach, Elapsed Time: " + sw.Elapsed.ToString());

        }

        public static void WorkflowAppCompleted(WorkflowApplicationCompletedEventArgs e)
        {
            if (e.CompletionState == ActivityInstanceState.Faulted)
            {
                Trace.WriteLine(string.Format("Workflow {0} Terminated.", e.InstanceId.ToString()));
                Trace.WriteLine(string.Format("Exception: {0}\n{1}",
                    e.TerminationException.GetType().FullName,
                    e.TerminationException.Message));
            }
            else if (e.CompletionState == ActivityInstanceState.Canceled)
            {
                Trace.WriteLine("Canceled!");
            }
            else
            {
                if (Interlocked.Decrement(ref numberOfThreadsRunning) == 0)
                {
                    eventDone.Set();
                }
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) DPKhttps://www.codeproject.com/script/Membership/M
Canada Canada
I am programmer and I love coding using C#, C++, and JavaScript. Smile | :)

Comments and Discussions