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

Nito.Disposables.SingleDisposable<T>

public abstract class SingleDisposable<T> : IDisposable
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 SingleDisposable<[System.Runtime.CompilerServices.Nullable(2)] T> : IDisposable { public bool IsDisposeStarted { get; } public bool IsDisposed { get; } public bool IsDisposing { get; } protected SingleDisposable(T context); protected abstract void Dispose(T context); public void Dispose(); protected bool TryUpdateContext(Func<T, T> contextUpdater); } }