TaskCompletionSource<TResult>.TrySetCanceled Method

Definition

Attempts to transition the underlying Task<TResult> into the Canceled state.

Overloads

TrySetCanceled()

Attempts to transition the underlying Task<TResult> into the Canceled state.

TrySetCanceled(CancellationToken)

Attempts to transition the underlying Task<TResult> into the Canceled state and enables a cancellation token to be stored in the canceled task.

TrySetCanceled()

Source:
TaskCompletionSource_T.cs
Source:
TaskCompletionSource_T.cs
Source:
TaskCompletionSource_T.cs

Attempts to transition the underlying Task<TResult> into the Canceled state.

C#
public bool TrySetCanceled();

Returns

true if the operation was successful; false if the operation was unsuccessful or the object has already been disposed.

Exceptions

The Task was disposed.

Remarks

This operation will return false if the Task<TResult> is already in one of the three final states: RanToCompletion, Faulted, or Canceled.

This method also returns false if the underlying Task<TResult> has already been disposed.

See also

Applies to

.NET 10 и друге верзије
Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

TrySetCanceled(CancellationToken)

Source:
TaskCompletionSource_T.cs
Source:
TaskCompletionSource_T.cs
Source:
TaskCompletionSource_T.cs

Attempts to transition the underlying Task<TResult> into the Canceled state and enables a cancellation token to be stored in the canceled task.

C#
public bool TrySetCanceled(System.Threading.CancellationToken cancellationToken);

Parameters

cancellationToken
CancellationToken

A cancellation token.

Returns

true if the operation is successful; otherwise, false.

Remarks

The method returns false if the underlying Task<TResult> object is already in one of the following three final states.

This method also returns false if the underlying Task<TResult> object has already been disposed.

Applies to

.NET 10 и друге верзије
Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0