Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parallel.Invoke
namespace ParallelInvoke
{
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
Parallel.Invoke(() =>
{
Console.WriteLine("Starting first task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoFirstTask();
}, () =>
{
Console.WriteLine("Starting second task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoSecondTask();
}, () =>
{
Console.WriteLine("Starting third task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoThirdTask();
}
);
Console.ReadLine();
}
public static void DoFirstTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Total numbers - {0}", numbers.Count());
Console.WriteLine("First task completed.");
}
public static void DoSecondTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Sum of all numbers - {0}", numbers.Sum());
Console.WriteLine("Second task completed.");
}
public static void DoThirdTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Average of all numbers - {0}", numbers.Average());
Console.WriteLine("Third task completed.");
}
}
}
namespace ParallelInvokeCancel
{
using System.Threading;
using System.Threading.Tasks;
class Program
{
static CancellationTokenSource cancelToken = new CancellationTokenSource();
static void Main(string[] args)
{
ParallelOptions options = new ParallelOptions();
options.CancellationToken = cancelToken.Token;
try
{
Parallel.Invoke(options, () =>
{
Console.WriteLine("Starting first task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoFirstTask();
}, () =>
{
Console.WriteLine("Starting second task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoSecondTask();
options.CancellationToken.ThrowIfCancellationRequested();
}, () =>
{
Console.WriteLine("Starting third task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoThirdTask();
}
);
}
catch (OperationCanceledException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error Message - {0}", ex.Message);
}
Console.ReadLine();
}
public static void DoFirstTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Total numbers - {0}", numbers.Count());
Console.WriteLine("First task completed.");
}
public static void DoSecondTask()
{
List<int> numbers = new List<int>();
if (numbers.Count <= 0)
{
cancelToken.Cancel();
return;
}
Console.WriteLine("Sum of all numbers - {0}", numbers.Sum());
Console.WriteLine("Second task completed.");
}
public static void DoThirdTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Average of all numbers - {0}", numbers.Average());
Console.WriteLine("Third task completed.");
}
}
}
namespace ParallelInvoke
{
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
class Program
{
public static ConcurrentQueue<Exception> exceptionQueue = new ConcurrentQueue<Exception>();
static void Main(string[] args)
{
try
{
Parallel.Invoke(() =>
{
Console.WriteLine("Starting first task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoFirstTask();
}, () =>
{
Console.WriteLine("Starting second task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoSecondTask();
if (exceptionQueue.Count > 0) throw new AggregateException(exceptionQueue);
}, () =>
{
Console.WriteLine("Starting third task");
Console.WriteLine("Thread ID - {0}", Thread.CurrentThread.ManagedThreadId);
DoThirdTask();
}
);
}
catch (AggregateException ex)
{
foreach (Exception e in ex.InnerExceptions)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error Message - {0}", e.InnerException.Message);
}
}
Console.ReadLine();
}
public static void DoFirstTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Total numbers - {0}", numbers.Count());
Console.WriteLine("First task completed.");
}
public static void DoSecondTask()
{
List<int> numbers = new List<int>();
if (numbers.Count <= 0)
{
exceptionQueue.Enqueue(new Exception("numbers list is empty"));
}
Console.WriteLine("Sum of all numbers - {0}", numbers.Sum());
Console.WriteLine("Second task completed.");
}
public static void DoThirdTask()
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Average of all numbers - {0}", numbers.Average());
Console.WriteLine("Third task completed.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment