static void Main(string[] args) { CancellationTokenSource cts = new CancellationTokenSource(); ThreadPool.QueueUserWorkItem(o => DoWork(cts.Token, 100)); Thread.Sleep(500); try { cts.Token.Register(CancelCallback3); cts.Token.Register(CancelCallback2); cts.Token.Register(CancelCallback1); cts.Cancel(false); } catch (AggregateException ex) { foreach (Exception curEx in ex.Data) { Trace.WriteLine(curEx.ToString()); } } Console.ReadKey(); } private static void CancelCallback1() { Trace.WriteLine("CancelCallback1 was called"); throw new Exception("CancellCallback1 exception"); } private static void CancelCallback2() { Trace.WriteLine("CancelCallback2 was called"); throw new Exception("CancellCallback2 exception"); } private static void CancelCallback3() { Trace.WriteLine("CancelCallback3 was called"); } private static void DoWork(CancellationToken cancellationToken, int maxLength) { int i = 0; while (i < maxLength && !cancellationToken.IsCancellationRequested) { Trace.WriteLine(i++); Thread.Sleep(100); } }输出为:01234CancelCallback1 was called根据http://msdn.microsoft.com/en-us/library/dd321703.aspx,我希望得到AggregateException,看来throwOnFirstException参数在这里没有任何意义。我的代码有什么问题。 最佳答案 您需要使用Task 类来获取AggregateException。它替代ThreadPool.QueueUserWorkItem()。关于c# - CancellationTokenSource.Cancel(false),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5299903/ 10-16 05:54