Represents an object that can be used to signal a.CancellationToken to stop an operation. This type supports a cooperative cancellation mechanism.

Syntax

public struct CancellationToken

Remarks

The CancellationToken struct is used to signal to operations that they should be cancelled. It's a lightweight, immutable struct that can be passed to methods that accept it as a parameter. Operations that wish to support cancellation should periodically check the CancellationToken.IsCancellationRequested property and throw an OperationCanceledException if it is true. You can also register callbacks with the token to be executed when cancellation is requested.

Cancellation is cooperative. A method that accepts a CancellationToken is not obligated to respond to cancellation requests. The method must periodically poll the token's state to determine if cancellation has been requested.

Fields

Name Description
None Gets a CancellationToken that is already canceled.

Properties

Name Description
CanBeCanceled Gets a value indicating whether this CancellationToken can be canceled.
IsCancellationRequested Gets a value indicating whether cancellation has been requested for this token.
IsCancellationRequested Gets a value indicating whether cancellation has been requested for this token.

Methods

Name Description
Register Registers a delegate that will be invoked when this token has been canceled.
ThrowIfCancellationRequested Throws an OperationCanceledException if cancellation has been requested for this token.

Constructors

Name Description
CancellationToken Initializes a new instance of the CancellationToken struct.

Operators

Name Description
== Determines whether two specified CancellationToken instances are equal.
!= Determines whether two specified CancellationToken instances are not equal.