Created
April 30, 2017 14:09
-
-
Save miteshsureja/d89a6faf323f5b9fff35d7a72bd00d1e to your computer and use it in GitHub Desktop.
Parallel.Invoke
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Parallel.Invoke – Task Parallel Library