|
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.