Class TaskExtensions
- Namespace
- Excalibur.Dispatch.Extensions
- Assembly
- Excalibur.Dispatch.dll
Provides extension methods for Task operations with timeout and cancellation support.
public static class TaskExtensions
- Inheritance
-
TaskExtensions
- Inherited Members
Methods
SafeAwaitAsync(Task)
Safely awaits a task and ignores any exceptions.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task SafeAwaitAsync(this Task task)
Parameters
taskTaskThe task to await.
Returns
- Task
A task that completes when the original task completes, regardless of success or failure.
SafeAwaitAsync<T>(Task<T>)
Safely awaits a task and returns default if it fails.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<T?> SafeAwaitAsync<T>(this Task<T> task)
Parameters
taskTask<T>The task to await.
Returns
- Task<T>
The task result, or default if the task fails.
Type Parameters
TThe type of the task result.
TimeoutAfterAsync(Task, TimeSpan)
Applies a timeout to the specified task.
public static Task TimeoutAfterAsync(this Task task, TimeSpan timeout)
Parameters
Returns
- Task
A task that represents the completion of the original task or a timeout exception.
Exceptions
- ArgumentNullException
Thrown when task is null.
- TimeoutException
Thrown when the operation does not complete within the specified timeout.
TimeoutAfterAsync<T>(Task<T>, TimeSpan)
Applies a timeout to the specified task that returns a value.
public static Task<T> TimeoutAfterAsync<T>(this Task<T> task, TimeSpan timeout)
Parameters
Returns
- Task<T>
A task that represents the completion of the original task with its result or a timeout exception.
Type Parameters
TThe type of the task result.
Exceptions
- ArgumentNullException
Thrown when task is null.
- TimeoutException
Thrown when the operation does not complete within the specified timeout.
WithCancellationAsync<T>(Task<T>, CancellationToken)
Executes a task with cancellation support, returning default if cancelled.
public static Task<T?> WithCancellationAsync<T>(this Task<T> task, CancellationToken cancellationToken)
Parameters
taskTask<T>The task to execute.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<T>
The task result, or default if cancelled.
Type Parameters
TThe type of the task result.
Exceptions
- ArgumentNullException
Thrown when task is null.