Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace CustomChannels
{
public static async Task<bool> WaitAsync(this AsyncAutoResetEvent source,
TimeSpan timeout, CancellationToken cancellationToken = default)
{
if (timeout < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout));
if (timeout == Timeout.InfiniteTimeSpan)
{
await source.WaitAsync(cancellationToken); return true;
}
cancellationToken.ThrowIfCancellationRequested();