Redigera

Dela via


CancellationTokenSource.TryReset Method

Definition

Attempts to reset the CancellationTokenSource to be used for an unrelated operation.

public:
 bool TryReset();
public bool TryReset ();
member this.TryReset : unit -> bool
Public Function TryReset () As Boolean

Returns

true if the CancellationTokenSource has not had cancellation requested and could have its state reset to be reused for a subsequent operation; otherwise, false.

Remarks

TryReset() is intended to be used by the sole owner of the CancellationTokenSource when it is known that:

  • The operation with which the CancellationTokenSource was used has completed.
  • No-one else will attempt to cancel it.
  • Any remaining registrations are erroneous.

Upon a successful reset, such registrations will no longer be notified for any subsequent cancellation of the CancellationTokenSource; however, if any component still holds a reference to this CancellationTokenSource either directly or indirectly via a CancellationToken handed out from it, polling via their reference will show the current state any time after the reset as it's the same instance. Usage of TryReset() concurrently with requesting cancellation is not thread-safe and may result in TryReset() returning true even if cancellation was already requested. Also, it may result in registrations not being invoked as part of the concurrent cancellation request.

Applies to