WaitWithTimeout 1.1.0

dotnet add package WaitWithTimeout --version 1.1.0
NuGet\Install-Package WaitWithTimeout -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WaitWithTimeout" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WaitWithTimeout --version 1.1.0
#r "nuget: WaitWithTimeout, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WaitWithTimeout as a Cake Addin
#addin nuget:?package=WaitWithTimeout&version=1.1.0

// Install WaitWithTimeout as a Cake Tool
#tool nuget:?package=WaitWithTimeout&version=1.1.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 TaskCanceledException 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): 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): 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 (TaskCanceledException 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
1.1.0 163 8/8/2023
1.0.0 149 8/8/2023