我从COLIN Mackay博客上的这篇文章中得到了以下代码。 Tasks that throw exceptions正如本文所暗示的那样,任务中抛出的异常不会被冒泡,除非调用其中一个等待...方法(不包括WaitAny)。我有2个类似的场景,给出两个不同的结果。
第一个场景
第二种情景
这是代码。
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Threading
{
class Program
{
static void Main(string[] args)
{
// Start the tasks
List<Task> tasks = new List<Task>();
for (int i = 0; i < 20; i++)
{
Task t = Task.Factory.StartNew(PerformTask);
tasks.Add(t);
}
Console.WriteLine("Press enter to display the task status.");
Console.ReadLine();
// Display the status of each task.
// If it has thrown an exception will be "faulted"
foreach (Task t in tasks)
Console.WriteLine("Task {0} status: {1}", t.Id, t.Status);
Console.WriteLine("Press enter to wait for all tasks.");
Console.ReadLine();
// This is where the AggregateException is finally thrown
Task.WaitAll(tasks.ToArray());
Console.ReadLine();
}
public static void PerformTask()
{
//comment this first
//string input = null;
//string output = input.ToUpper();
Console.WriteLine("Starting Task {0}", Task.CurrentId);
//comment this second
throw new Exception("Throwing exception in task " + Task.CurrentId);
}
}
}
答案 0 :(得分:3)
差异在于任务没有启动,因为控制台没有 将任务的状态显示为第一个场景
那是因为第二次,在Console.WriteLine
方法调用之前抛出了异常:
string input = null;
string output = input.ToUpper(); // this will throw
Console.WriteLine("Starting Task {0}", Task.CurrentId);
对战:
Console.WriteLine("Starting Task {0}", Task.CurrentId); // This will first be written
throw new Exception("Throwing exception in task " + Task.CurrentId);
如果您想体验相同的行为,请在方法开头写到控制台:
public static void PerformTask()
{
Console.WriteLine("Starting Task {0}", Task.CurrentId);
//string input = null;
//string output = input.ToUpper();
throw new Exception("Throwing exception in task " + Task.CurrentId);
}