Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

class System.​Threading.​CancellationTokenSource

Assembly: System.Runtime

Inheritance: object → CancellationTokenSource

Implemented Interfaces

Signals to a <see cref="T:System.Threading.CancellationToken" /> that it should be canceled.

Properties

public bool
IsCancellationRequested
Gets whether cancellation has been requested for this <see cref="T:System.Threading.CancellationTokenSource" /> .
Token
Gets the <see cref="T:System.Threading.CancellationToken" /> associated with this <see cref="T:System.Threading.CancellationTokenSource" /> .

Methods

public void
Cancel​()
Communicates a request for cancellation.
public void
Cancel​(bool throwOnFirstException)
Communicates a request for cancellation, and specifies whether remaining callbacks and cancelable operations should be processed if an exception occurs.
throwOnFirstException <see langword="true" /> if exceptions should immediately propagate; otherwise, <see langword="false" /> .
public void
CancelAfter​(int millisecondsDelay)
Schedules a cancel operation on this <see cref="T:System.Threading.CancellationTokenSource" /> after the specified number of milliseconds.
millisecondsDelay The time span to wait before canceling this <see cref="T:System.Threading.CancellationTokenSource" /> .
public void
CancelAfter​(TimeSpan delay)
Schedules a cancel operation on this <see cref="T:System.Threading.CancellationTokenSource" /> after the specified time span.
delay The time span to wait before canceling this <see cref="T:System.Threading.CancellationTokenSource" /> .
public Task
CancelAsync​()
Communicates a request for cancellation asynchronously.
Returns A task that will complete after cancelable operations and callbacks registered with the associated <see cref="T:System.Threading.CancellationToken" /> have completed.
public static CancellationTokenSource
CreateLinkedTokenSource​(CancellationToken token)
Creates a <see cref="T:System.Threading.CancellationTokenSource" /> that will be in the canceled state when the supplied token is in the canceled state.
Returns An object that's linked to the source token.
token The cancellation token to observe.
public static CancellationTokenSource
CreateLinkedTokenSource​(CancellationToken token1, CancellationToken token2)
Creates a <see cref="T:System.Threading.CancellationTokenSource" /> that will be in the canceled state when any of the source tokens are in the canceled state.
Returns A <see cref="T:System.Threading.CancellationTokenSource" /> that is linked to the source tokens.
token1 The first cancellation token to observe.
token2 The second cancellation token to observe.
public static CancellationTokenSource
CreateLinkedTokenSource​(ReadOnlySpan<​CancellationToken> tokens)
Creates a <see cref="T:System.Threading.CancellationTokenSource" /> that will be in the canceled state when any of the source tokens are in the canceled state.
Returns A <see cref="T:System.Threading.CancellationTokenSource" /> that is linked to the source tokens.
tokens The <see cref="T:System.Threading.CancellationToken">CancellationToken</see> instances to observe.
public static CancellationTokenSource
CreateLinkedTokenSource​(CancellationToken[] tokens)
Creates a <see cref="T:System.Threading.CancellationTokenSource" /> that will be in the canceled state when any of the source tokens in the specified array are in the canceled state.
Returns A <see cref="T:System.Threading.CancellationTokenSource" /> that is linked to the source tokens.
tokens An array that contains the cancellation token instances to observe.
public void
Dispose​()
Releases all resources used by the current instance of the <see cref="T:System.Threading.CancellationTokenSource" /> class.
protected void
Dispose​(bool disposing)
Releases the unmanaged resources used by the <see cref="T:System.Threading.CancellationTokenSource" /> class and optionally releases the managed resources.
disposing <see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.
public bool
TryReset​()
Attempts to reset the <see cref="T:System.Threading.CancellationTokenSource" /> to be used for an unrelated operation.
Returns <see langword="true" /> if the <see cref="T:System.Threading.CancellationTokenSource" /> has not had cancellation requested and could have its state reset to be reused for a subsequent operation; otherwise, <see langword="false" /> .
public bool
Equals​(object obj)
Inherited from object
Determines whether the specified object is equal to the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
obj The object to compare with the current object.
protected void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public int
GetHashCode​()
Inherited from object
Serves as the default hash function.
Returns A hash code for the current object.
public Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .
public string
ToString​()
Inherited from object
Returns a string that represents the current object.
Returns A string that represents the current object.