ResettableCancellationTokenSource Class

Represents a cancellation token source that can be reset to its initial state.

public sealed class ResettableCancellationTokenSource : IAsyncDisposable, IDisposable

Remarks

All members are safe to call concurrently. Note that the callbacks registered on ResettableCancellationTokenSource.Token run while the internal lock is held, so a callback must not block waiting on another thread that uses the same instance. ResettableCancellationTokenSource.DisposeAsync is the exception: it runs them on another thread and does not hold the lock while they complete.

ResettableCancellationTokenSource.Reset reuses the underlying CancellationTokenSource when it can, so whether a CancellationToken obtained before the reset belongs to the previous generation depends on the state of that source:

Read ResettableCancellationTokenSource.Token again after resetting rather than relying on either behavior.

Example

using var cts = new ResettableCancellationTokenSource(cancelOnResetAndDispose: true);

// Use the token
var token = cts.Token;
await Task.Delay(1, token);

// Reset to reuse: the previous token is canceled, read Token again to get a new one
cts.Reset();
Assert.True(token.IsCancellationRequested);
await Task.Delay(1, cts.Token);
Implements

Constructors

NameDescription
ResettableCancellationTokenSource(ResettableCancellationTokenSourceOptions)Initializes a new instance of the ResettableCancellationTokenSource class with the specified options.
ResettableCancellationTokenSource(bool)Initializes a new instance of the ResettableCancellationTokenSource class.

ResettableCancellationTokenSource overloads

ResettableCancellationTokenSource(ResettableCancellationTokenSourceOptions)

ResettableCancellationTokenSource.cs

Initializes a new instance of the ResettableCancellationTokenSource class with the specified options.

public ResettableCancellationTokenSource(ResettableCancellationTokenSourceOptions options)
Parameters
options ResettableCancellationTokenSourceOptions
Options that control the behavior when resetting or disposing.

ResettableCancellationTokenSource(bool)

ResettableCancellationTokenSource.cs

Initializes a new instance of the ResettableCancellationTokenSource class.

public ResettableCancellationTokenSource(bool cancelOnResetAndDispose)
Parameters
cancelOnResetAndDispose bool
true to cancel the token when resetting or disposing; otherwise, false.

Properties

NameDescription
IsCancellationRequestedGets whether cancellation has been requested for this token source.
TokenGets the cancellation token associated with this ResettableCancellationTokenSource.

Gets whether cancellation has been requested for this token source.

public bool IsCancellationRequested { get; }

Property value

bool

Gets the cancellation token associated with this ResettableCancellationTokenSource.

public CancellationToken Token { get; }

Property value

CancellationToken

Methods

NameDescription
Cancel()Communicates a request for cancellation.
CancelAfter(TimeSpan)Schedules a cancel operation on this ResettableCancellationTokenSource after the specified time span.
Dispose()Releases the resources used by this ResettableCancellationTokenSource.
DisposeAsync()Releases the resources used by this ResettableCancellationTokenSource, without running the cancellation callbacks on the calling thread.
Reset()Resets the cancellation token source to its initial state.

Communicates a request for cancellation.

public void Cancel()

Schedules a cancel operation on this ResettableCancellationTokenSource after the specified time span.

public void CancelAfter(TimeSpan delay)

Parameters

delay TimeSpan
The time span to wait before canceling this ResettableCancellationTokenSource.

Releases the resources used by this ResettableCancellationTokenSource.

public void Dispose()

Exceptions

AggregateException
A callback registered on the current ResettableCancellationTokenSource.Token threw.

Remarks

The underlying CancellationTokenSource is disposed even when a cancellation callback throws: the exception raised by CancellationTokenSource.Cancel propagates to the caller only after the resources are released. Subsequent calls do nothing.

Releases the resources used by this ResettableCancellationTokenSource, without running the cancellation callbacks on the calling thread.

public ValueTask DisposeAsync()

Returns

ValueTask

Exceptions

AggregateException
A callback registered on the current ResettableCancellationTokenSource.Token threw.

Remarks

Unlike ResettableCancellationTokenSource.Dispose, this method relies on CancellationTokenSource.CancelAsync, so the callbacks registered on ResettableCancellationTokenSource.Token run on another thread and are awaited instead of blocking the caller. A callback is therefore free to use this instance, which is not the case with ResettableCancellationTokenSource.Dispose.

The underlying CancellationTokenSource is disposed even when a callback throws: the exception raised by CancellationTokenSource.CancelAsync propagates to the caller only after the resources are released. Subsequent calls do nothing, whether they go through ResettableCancellationTokenSource.Dispose or this method, but a call made while this one is still awaiting the callbacks returns before the resources are released.

Resets the cancellation token source to its initial state.

public void Reset()

Exceptions

ObjectDisposedException
The instance is disposed.
AggregateException
A callback registered on the current ResettableCancellationTokenSource.Token threw.

Remarks

The underlying CancellationTokenSource is reused when cancellation has not been requested, so the token read before the reset can be the very token ResettableCancellationTokenSource.Token returns afterwards. See the remarks on ResettableCancellationTokenSource for the exact behavior.

The instance is reset even when a cancellation callback throws: the exception raised by CancellationTokenSource.Cancel propagates to the caller only after a fresh ResettableCancellationTokenSource.Token is available. A callback that disposes this instance wins, and the reset is abandoned.