A 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- Timeout Support: Execute async operations with configurable timeout handling
- Multi-Framework Support: Compatible with .NET Standard 2.0, .NET 8.0, .NET 9.0, and .NET 10.0
- Lightweight: Minimal dependencies, focused on essential functionality
- Modern: Supports both
TaskandValueTaskpatterns
Execute 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. UseTimeout.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. UseTimeout.InfiniteTimeSpanto 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 thanTimeout.InfiniteTimeSpan.OperationCanceledException: If the operation is cancelled via the cancellation token.
- Check the return value: Always check if the operation completed successfully before accessing results.
- Use TimeSpan for clarity: Prefer
TimeSpanoverloads for better readability in production code. - Handle cancellation: Consider using
CancellationTokenfor graceful shutdown scenarios. - Don't ignore timeouts: Log or handle timeout scenarios appropriately in your application.
- NetEvolve.Arguments - Argument validation library used internally
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.