Waiting on a Task with a OnlyOnFaulted Continuation causes an AggregateException

You’re not waiting on a task with an OnlyOnFaulted continuation – you’re waiting on that continuation (returned by ContinueWith). The continuation is never going to fire because the original task returned normally, so it’s acting as if it were cancelled.

Makes sense to me.

I suspect you want to create the task, add the continuation, but then wait on the original task:

var taskTest = Task.Factory.StartNew(() =>
{
    System.Threading.Thread.Sleep(5000);

});
taskTest.ContinueWith((Task t) =>
{
    Console.WriteLine("ERR");
}, TaskContinuationOptions.OnlyOnFaulted);

Leave a Comment