METHOD

TaskCompletionSource<T>.TrySetCanceled

Attempts to mark a pending task canceled and associates the supplied token. Returns false if a terminal outcome already exists. This changes the task outcome, not the state of the token source.

Formal Syntax & Structure

csharp syntaxcsharp
source.TrySetCanceled(token)

Working Example

Example Snippetcsharp
using System;using System.Threading;using System.Threading.Tasks;var gate = new TaskCompletionSource<int>(); Console.WriteLine(gate.TrySetCanceled(CancellationToken.None)); Console.WriteLine(gate.Task.IsCanceled); // True then True
C# & .NET Reference