<PackageReference Include="Nito.Disposables" Version="2.2.1" />

Nito.Disposables.SingleAsyncDisposable<T>

public abstract class SingleAsyncDisposable<T> : IAsyncDisposable
A base class for disposables that need exactly-once semantics in a thread-safe way. All disposals of this instance block until the disposal is complete.
namespace Nito.Disposables { public abstract class SingleAsyncDisposable<[System.Runtime.CompilerServices.Nullable(2)] T> : IAsyncDisposable { public bool IsDisposeStarted { get; } public bool IsDisposed { get; } public bool IsDisposing { get; } protected SingleAsyncDisposable(T context); protected abstract ValueTask DisposeAsync(T context); public ValueTask DisposeAsync(); protected bool TryUpdateContext(Func<T, T> contextUpdater); } }