CancellationTokenSource Constructors

Definition

Initializes the CancellationTokenSource.

Overloads

CancellationTokenSource()

Initializes a new instance of the CancellationTokenSource class.

CancellationTokenSource(Int32)

Initializes a new instance of the CancellationTokenSource class that will be canceled after the specified delay in milliseconds.

CancellationTokenSource(TimeSpan)

Initializes a new instance of the CancellationTokenSource class that will be canceled after the specified time span.

CancellationTokenSource(TimeSpan, TimeProvider)

Initializes a new instance of the CancellationTokenSource class that will be canceled after the specified TimeSpan.

CancellationTokenSource()

Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs

Initializes a new instance of the CancellationTokenSource class.

C#
public CancellationTokenSource();

See also

Applies to

.NET 10 and other versions
Product Versions
.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

CancellationTokenSource(Int32)

Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs

Initializes a new instance of the CancellationTokenSource class that will be canceled after the specified delay in milliseconds.

C#
public CancellationTokenSource(int millisecondsDelay);

Parameters

millisecondsDelay
Int32

The time interval in milliseconds to wait before canceling this CancellationTokenSource.

Exceptions

millisecondsDelay is less than -1.

Remarks

The countdown for the millisecondsDelay starts during the call to the constructor. When the millisecondsDelay expires, the constructed CancellationTokenSource is canceled (if it has not been canceled already).

Subsequent calls to CancelAfter will reset the millisecondsDelay for the constructed CancellationTokenSource, if it has not been canceled already.

Applies to

.NET 10 and other versions
Product Versions
.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.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

CancellationTokenSource(TimeSpan)

Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs

Initializes a new instance of the CancellationTokenSource class that will be canceled after the specified time span.

C#
public CancellationTokenSource(TimeSpan delay);

Parameters

delay
TimeSpan

The time interval to wait before canceling this CancellationTokenSource.

Exceptions

delay.TotalMilliseconds is less than -1 or greater than Int32.MaxValue (or UInt32.MaxValue - 1 on some versions of .NET). Note that this upper bound is more restrictive than TimeSpan.MaxValue.

Remarks

The countdown for the delay starts during the call to the constructor. When the delay expires, the constructed CancellationTokenSource is canceled, if it has not been canceled already.

Subsequent calls to CancelAfter will reset the delay for the constructed CancellationTokenSource, if it has not been canceled already.

Applies to

.NET 10 and other versions
Product Versions
.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.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

CancellationTokenSource(TimeSpan, TimeProvider)

Source:
CancellationTokenSource.cs
Source:
CancellationTokenSource.cs

Initializes a new instance of the CancellationTokenSource class that will be canceled after the specified TimeSpan.

C#
public CancellationTokenSource(TimeSpan delay, TimeProvider timeProvider);

Parameters

delay
TimeSpan

The time interval to wait before canceling this CancellationTokenSource.

timeProvider
TimeProvider

The TimeProvider with which to interpret the delay.

Exceptions

delay's TotalMilliseconds is less than -1 or greater than MaxValue - 1.

timeProvider is null.

Remarks

The countdown for the delay starts during the call to the constructor. When the delay expires, the constructed CancellationTokenSource is canceled, if it has not been canceled already. Subsequent calls to CancelAfter will reset the delay for the constructed CancellationTokenSource, if it has not been canceled already.

Applies to

.NET 10 and other versions
Product Versions
.NET 8, 9, 10