public static void Main() { var result = new ConcurrentBag <int>(); Action <Example3Args> method = args => { for (int i = args.Start; i < args.Start + args.NumbersCount; i++) { args.Result.Add(i); } }; var args1 = new Example3Args() { Start = 0, NumbersCount = 50, Result = result }; var args2 = new Example3Args() { Start = 50, NumbersCount = 50, Result = result }; Parallel.Invoke( () => method.Invoke(args1), () => method.Invoke(args2) ); foreach (var i in result) { Console.WriteLine(i); } }
public static void Main() { var allDone = new ManualResetEvent(false); var result = new ConcurrentBag <int>(); var completedTasks = 0; var threadCounts = 2; Action <Example3Args> method = args => { for (int i = args.Start; i < args.Start + args.NumbersCount; i++) { args.Result.Add(i); } if (Interlocked.Increment(ref completedTasks) == threadCounts) { allDone.Set(); } }; var args1 = new Example3Args() { Start = 0, NumbersCount = 50, Result = result }; var args2 = new Example3Args() { Start = 50, NumbersCount = 50, Result = result }; ThreadPool.QueueUserWorkItem(method, args1, false); ThreadPool.QueueUserWorkItem(method, args2, false); allDone.WaitOne(); foreach (var i in result) { Console.WriteLine(i); } }
public static void Main() { var result = new ConcurrentBag <int>(); ParameterizedThreadStart method = obj => { var args = (Example3Args)obj; for (int i = args.Start; i < args.Start + args.NumbersCount; i++) { args.Result.Add(i); } }; var thread1 = new Thread(method); var thread2 = new Thread(method); var args1 = new Example3Args() { Start = 0, NumbersCount = 50, Result = result }; var args2 = new Example3Args() { Start = 50, NumbersCount = 50, Result = result }; thread1.Start(args1); thread2.Start(args2); thread1.Join(); thread2.Join(); foreach (var i in result) { Console.WriteLine(i); } }