Table of Contents

Class CancellationTokenExtensions

Namespace
Plugin.BaseTypeExtensions
Assembly
Plugin.BaseTypeExtensions.dll

Provides extension methods for CancellationToken operations and utility functions.

public static class CancellationTokenExtensions
Inheritance
CancellationTokenExtensions
Inherited Members

Methods

AsTask(CancellationToken)

Converts the CancellationToken to a Task that completes when the token is cancelled.

public static Task AsTask(this CancellationToken token)

Parameters

token CancellationToken

The CancellationToken to convert.

Returns

Task

A Task that completes when the token is cancelled.

ThrowIfCanceledAfter(CancellationToken, TimeSpan)

Throws an OperationCanceledException if cancellation is requested after the specified timeout.

public static void ThrowIfCanceledAfter(this CancellationToken token, TimeSpan timeout)

Parameters

token CancellationToken

The CancellationToken to check.

timeout TimeSpan

The timeout duration to wait before throwing.

Exceptions

OperationCanceledException

Thrown when the token is cancelled or timeout expires.

ArgumentOutOfRangeException

Thrown when timeout is negative.

WaitHandle(CancellationToken, int)

Waits for the CancellationToken to be signaled with a timeout.

public static bool WaitHandle(this CancellationToken token, int timeoutMs)

Parameters

token CancellationToken

The CancellationToken to wait for.

timeoutMs int

The timeout in milliseconds.

Returns

bool

True if the token was signaled within the timeout; otherwise, false.

Exceptions

ArgumentOutOfRangeException

Thrown when timeoutMs is negative.

WithTimeout(CancellationToken, int)

Creates a new CancellationToken that will be cancelled after the specified timeout in milliseconds.

public static CancellationToken WithTimeout(this CancellationToken token, int timeoutMs)

Parameters

token CancellationToken

The original CancellationToken.

timeoutMs int

The timeout in milliseconds.

Returns

CancellationToken

A CancellationToken that combines the original token with a timeout.

Exceptions

ArgumentOutOfRangeException

Thrown when timeoutMs is negative.

WithTimeout(CancellationToken, TimeSpan)

Creates a new CancellationToken that will be cancelled after the specified timeout.

public static CancellationToken WithTimeout(this CancellationToken token, TimeSpan timeout)

Parameters

token CancellationToken

The original CancellationToken.

timeout TimeSpan

The timeout duration.

Returns

CancellationToken

A CancellationToken that combines the original token with a timeout.

Exceptions

ArgumentOutOfRangeException

Thrown when timeout is negative.