1

When I set the CancellationTokenSource to cancel after 5 seconds. The TaskCompletionSource will not be cancelled.

[Test]
public async Task Test()
{
  var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));      
  var completionSource = new TaskCompletionSource(cts.Token);

  await completionSource.Task;
}
Rene
  • 526
  • 6
  • 18
  • May be helpful https://stackoverflow.com/questions/39897151/how-to-combine-taskcompletionsource-and-cancellationtokensource, mostly this part https://stackoverflow.com/a/39902447/2011071 – Serg Jan 18 '22 at 11:38

1 Answers1

4

The TaskCompletionSource object doesn't take cancellation tokens in the constructor, it takes any object. It's not supposed to listen to cancellation tokens.

You can read the MSDN article to see what the constructor does and how to use the TaskCompletionSource object.

You probably want to use the SetCanceled method.

asaf92
  • 1,557
  • 1
  • 19
  • 30