Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Created April 30, 2017 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miteshsureja/d89a6faf323f5b9fff35d7a72bd00d1e to your computer and use it in GitHub Desktop.
Save miteshsureja/d89a6faf323f5b9fff35d7a72bd00d1e to your computer and use it in GitHub Desktop.
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.");
}
}
}
@miteshsureja
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment