This library provides simple extension methods for `Task`, `Task<T>`, `ValueTask` and `ValueTask<T>`.
$ dotnet add package NetEvolve.Extensions.TasksA lightweight library providing extension methods for Task, Task<T>, ValueTask, and ValueTask<T> to simplify asynchronous programming patterns in .NET applications.
Part of the Daily DevOps & .NET - NetEvolve project, which aims to provide a set of useful libraries for .NET developers.
dotnet add package NetEvolve.Extensions.Tasks
Task and ValueTask patternsExecute asynchronous operations with a timeout. Returns true if the operation completes within the specified timeout, false otherwise.
using NetEvolve.Extensions.Tasks;
var task = SomeAsyncOperation();
var completed = await task.WithTimeoutAsync(5000); // 5 seconds timeout
if (completed)
{
Console.WriteLine("Operation completed successfully");
}
else
{
Console.WriteLine("Operation timed out");
}
using NetEvolve.Extensions.Tasks;
var task = SomeAsyncOperation();
var completed = await task.WithTimeoutAsync(TimeSpan.FromSeconds(5));
if (completed)
{
Console.WriteLine("Operation completed successfully");
}
else
{
Console.WriteLine("Operation timed out");
}
using NetEvolve.Extensions.Tasks;
var task = GetDataAsync();
var completed = await task.WithTimeoutAsync(3000);
if (completed)
{
var result = await task; // Safe to await again, already completed
Console.WriteLine($"Result: {result}");
}
else
{
Console.WriteLine("Operation timed out");
}
using NetEvolve.Extensions.Tasks;
ValueTask operation = PerformAsyncOperation();
var completed = await operation.WithTimeoutAsync(TimeSpan.FromSeconds(10));
if (!completed)
{
Console.WriteLine("Operation did not complete in time");
}
using NetEvolve.Extensions.Tasks;
var cts = new CancellationTokenSource();
var task = LongRunningOperation();
try
{
var completed = await task.WithTimeoutAsync(5000, cts.Token);
if (!completed)
{
Console.WriteLine("Timeout occurred");
}
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation was cancelled");
}
All extension methods are available for:
TaskTask<T>ValueTaskValueTask<T>Waits for a task to complete within the specified timeout in milliseconds.
Parameters:
timeoutInMilliseconds (int): Timeout in milliseconds. Use Timeout.Infinite (-1) to wait indefinitely.cancellationToken (CancellationToken): Optional cancellation token.Returns: Task<bool> - true if the operation completed within the timeout, false otherwise.
Exceptions:
ArgumentNullException: If the task is null.ArgumentOutOfRangeException: If timeout is less than -1.OperationCanceledException: If the operation is cancelled via the cancellation token.Waits for a task to complete within the specified timeout.
Parameters:
timeout (TimeSpan): Timeout duration. Use Timeout.InfiniteTimeSpan to wait indefinitely.cancellationToken (CancellationToken): Optional cancellation token.Returns: Task<bool> - true if the operation completed within the timeout, false otherwise.
Exceptions:
ArgumentNullException: If the task is null.ArgumentOutOfRangeException: If timeout is less than Timeout.InfiniteTimeSpan.OperationCanceledException: If the operation is cancelled via the cancellation token.TimeSpan overloads for better readability in production code.CancellationToken for graceful shutdown scenarios.This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues, questions, or contributions, please visit the GitHub repository.