WaitWithTimeout 2.0.0

dotnet add package WaitWithTimeout --version 2.0.0
                    
NuGet\Install-Package WaitWithTimeout -Version 2.0.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="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WaitWithTimeout" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="WaitWithTimeout" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WaitWithTimeout --version 2.0.0
                    
#r "nuget: WaitWithTimeout, 2.0.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.
#:package WaitWithTimeout@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WaitWithTimeout&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=WaitWithTimeout&version=2.0.0
                    
Install as a Cake Tool

Nuget GitHub GitHub contributors GitHub Workflow Status (with event)

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;
    }
}

Contribution

👍 You are encouraged to contribute to this project by forking it or giving it a star if you find it valuable 😃

Social Media

Email Stack Overflow Linkedin Twitter Follow

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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
2.0.0 144 1/26/2025
1.1.0 239 8/8/2023
1.0.0 218 8/8/2023