<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.
public bool IsDisposed { get; }

Whether this instance is disposed (finished disposing).

public bool IsDisposeStarted { get; }

Whether this instance is currently disposing or has been disposed.

public bool IsDisposing { get; }

Whether this instance is currently disposing, but not finished yet.

protected SingleAsyncDisposable(T context)

Creates a disposable for the specified context.

protected abstract ValueTask DisposeAsync(T context)

The actul disposal method, called only once from DisposeAsync.

Disposes this instance.

protected bool TryUpdateContext(Func<T, T> contextUpdater)

Attempts to update the stored context. This method returns false if this instance has already been disposed (or is being disposed).