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

Nito.Disposables.SingleNonblockingDisposable<T>

public abstract class SingleNonblockingDisposable<T> : IDisposable
A base class for disposables that need exactly-once semantics in a thread-safe way.
public bool IsDisposed { get; }

Whether this instance has been disposed (or is being disposed).

protected SingleNonblockingDisposable(T context)

Creates a disposable for the specified context.

protected abstract void Dispose(T context)

The actul disposal method, called only once from Dispose.

public void Dispose()

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).