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
CancellationTokenThe 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
CancellationTokenThe CancellationToken to check.
timeout
TimeSpanThe 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
CancellationTokenThe CancellationToken to wait for.
timeoutMs
intThe 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
CancellationTokenThe original CancellationToken.
timeoutMs
intThe 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
CancellationTokenThe original CancellationToken.
timeout
TimeSpanThe timeout duration.
Returns
- CancellationToken
A CancellationToken that combines the original token with a timeout.
Exceptions
- ArgumentOutOfRangeException
Thrown when timeout is negative.