null created by belain - https://repl.it/DvzS/0
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
namespace Rextester
{
public class ParallelDemo
{
private static Stopwatch stopWatch = new Stopwatch();
public static void Run1()
{
Thread.Sleep(2000);
Console.WriteLine("Task 1 is cost 2 sec");
throw new Exception("Exception in task 1");
}
public static void Run2()
{
Thread.Sleep(3000);
Console.WriteLine("Task 2 is cost 3 sec");
throw new Exception("Exception in task 2");
}
public static void Run3(int i) {
Console.WriteLine("Task 3 is cost " + i + " sec");
}
public static void Main()
{
stopWatch.Start();
// Parallel.Invoke(Run1, Run2);
try
{
Parallel.Invoke(Run1, Run2);
}
catch (AggregateException aex)
{
foreach (var ex in aex.InnerExceptions)
{
Console.WriteLine(ex.Message);
}
}
Parallel.For(0, 10, item => {
Run3(item);
});
stopWatch.Stop();
Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms.");
stopWatch.Restart();
try {
Run1();
Run2(); // 捕获到Run1()
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
stopWatch.Stop();
Console.WriteLine("Normal run " + stopWatch.ElapsedMilliseconds + " ms.");
}
}
}