lobsterism
lobsterism

Reputation: 3509

TaskCompletionSource cancellation with CancellationTokenSource does not update Task.Status?

This seems unintuitive to me:

var cts = new CancellationTokenSource();
cts.Cancel();
var tcs = new TaskCompletionSource<int>();
try
{
    tcs.Task.Wait(cts.Token);
}
catch (OperationCanceledException)
{   
    Console.WriteLine(tcs.Task.Status); //TaskStatus.WaitingForActivation
}

I'd expect it to update the status to TaskStatus.Canceled. What is the rationale behind leaving it in TaskStatus.WaitingForActivation?

Upvotes: 0

Views: 680

Answers (1)

SLaks
SLaks

Reputation: 888177

You're misunderstanding Wait().

Wait(CancellationToken) allows you to cancel the wait operation.
It has no effect on the underlying task.

Upvotes: 3

Related Questions