这里是一个示例代码:
输出结果(顺序无关紧要):
var task = Task.Factory.StartNew(() => { throw new Exception(); });
task.ContinueWith(t => Console.WriteLine("Exception"), TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith(t => Console.WriteLine("Success"), TaskContinuationOptions.NotOnFaulted)
.ContinueWith(t => Console.WriteLine("Should not be executed. Task status = " + t.Status, TaskContinuationOptions.NotOnCanceled));
Console.ReadLine();
输出结果(顺序无关紧要):
为什么第二个ContinueWith被执行了,如何防止它?异常
不应该执行。任务状态=已取消
params object[]
参数等而可能悄然产生的微妙问题之一。 - Timothy Shields