I have tried many ways but failed to catch the cancellation exception in task.ContinueWith. Is there anything wrong here:
CancellationTokenSource tokenSource = new CancellationTokenSource();
Task task = new Task( ()=> { Thread.Sleep(1000); Console.WriteLine("in task!"); }, tokenSource.Token);
task.Start();
tokenSource.Cancel();
task.ContinueWith(t =>
{
if(t.IsCanceled)
{
AggregateException e = t.Exception;
if(e == null) // is true
Console.WriteLine("Cancelled: ");
}
});
Console.Read();
The output is :
Cancelled:
which implies the cancellation exception is caught but the exception itself is empty. My question is how to get the cancellation exception here?
Thanks
Derek