WaitWithTimeout 1.0.0
See the version list below for details.
dotnet add package WaitWithTimeout --version 1.0.0
NuGet\Install-Package WaitWithTimeout -Version 1.0.0
<PackageReference Include="WaitWithTimeout" Version="1.0.0" />
paket add WaitWithTimeout --version 1.0.0
#r "nuget: WaitWithTimeout, 1.0.0"
// Install WaitWithTimeout as a Cake Addin
#addin nuget:?package=WaitWithTimeout&version=1.0.0
// Install WaitWithTimeout as a Cake Tool
#tool nuget:?package=WaitWithTimeout&version=1.0.0
Async Timeout Handling
This project provides methods to handle asynchronous operations with timeout capabilities.
Overview
This project contains extension methods for handling asynchronous operations with timeouts. The primary purpose of these methods is to provide a way to execute asynchronous tasks while ensuring that they complete within a specified time frame. If the task takes longer than the specified timeout, a TimeoutException
is thrown.
Usage
You can use this project to wrap your asynchronous tasks with timeout constraints. The class provides the following methods:
WaitWithTimeoutAsync<T>(this Task<T> task, TimeSpan timeout, string exceptionMessage = "The operation has timed out.")
: Wraps an asynchronous task that returns a result with a timeout. If the task completes within the specified timeout, the result is returned. Otherwise, a TimeoutException is thrown.
WaitWithTimeoutAsync(this Task task, TimeSpan timeout, string exceptionMessage = "The operation has timed out.")
: Wraps an asynchronous task without a return value with a timeout. If the task completes within the specified timeout, the execution continues. Otherwise, a TimeoutException is thrown.
WaitWithTimeoutAsync<T>(this ValueTask<T> task, TimeSpan timeout)
: Wraps a ValueTask<T> with a timeout. This method internally converts the ValueTask<T> to a regular Task<T> and uses the first method mentioned for timeout handling.
Here's an example of how to use the methods:
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var timeout = TimeSpan.FromSeconds(5);
try
{
var result = await SomeAsyncOperation().WaitWithTimeoutAsync(timeout);
Console.WriteLine("Operation succeeded: " + result);
}
catch (TimeoutException ex)
{
Console.WriteLine("Operation timed out: " + ex.Message);
}
}
static async Task<int> SomeAsyncOperation()
{
await Task.Delay(TimeSpan.FromSeconds(3)); // Simulating an async operation
return 42;
}
}
Contributions
Contributions to this project are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.
License
This utility class is provided under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.