为什么这不是每一个都完成?相反,它会引发异常,就像未捕获异常一样。此外,异常“索引超出数组范围”对我来说没有意义。
int n = 3;
string[] names = new string[] { "sally", "fred", "gina" };
Task[] myTasks = new Task[n];
for (int y = 0; y < n; y++)
{
myTasks[y] = Task.Factory.StartNew(() =>
{
Action<string> action = (name) =>
{
try
{
throw new Exception(name + ", I bet you can't catch me.");
}
catch (Exception e)
{
//I caught you... didn't I?
}
};
action(names[y]);
});
}
Task.WaitAll(myTasks);
Console.WriteLine("All tasks complete.");//This line is never reached;