Skip to content

kygagner/Disposables

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This fork exists for purposes of strong naming.

Disposables Build status codecov NuGet version API docs

IDisposable helper types.

AnonymousDisposable

The AnonymousDisposable type wraps an Action, and invokes that Action exactly once when it is disposed. The first thread to call Dispose is the one that invokes the Action; all other threads that call Dispose are blocked until the Action is completed. Once the Action is completed, it is never invoked again; future calls to AnonymousDisposable.Dispose are no-ops.

You can create an AnonymousDisposable by calling AnonymousDisposable.Create(Action) or new AnonymousDisposable(Action).

Advanced

You can append an Action to an AnonymousDisposable by calling its Add method with the Action to add. If the AnonymousDisposable is already disposed (or is in the process of being disposed by another thread), then the additional Action is invoked immediately.

CollectionDisposable

CollectionDisposable contains a collection of IDisposable instances, and disposes them all exactly once when it is disposed. The first thread to call Dispose is the one that disposes all instances; all other threads that call Dispose are blocked until all instances have been disposed. Once disposed, future calls to CollectionDisposable.Dispose are no-ops.

You can create a CollectionDisposable by calling CollectionDisposable.Create(...) or new CollectionDisposable(...), passing the collection of disposables.

You can also append a disposable to the CollectionDisposable by calling its Add method and passing it the disposable. If the CollectionDisposable is already disposed (or is in the process of being disposed by another thread), then the additional disposable is disposed immediately.

NoopDisposable

An IDisposable that does nothing when disposed.

You can retrieve the singleton instance via NoopDisposable.Instance.

Advanced Types

SingleDisposable<T>

The SingleDisposable<T> type is a base type for disposables that desire exactly-once semantics, blocking other threads calling Dispose until the initial Dispose is complete. Both AnonymousDisposable and CollectionDisposable inherit from this type.

The type T is an immutable type that represents the contextual state of the instance. It is initialized in the constructor, optionally updated by calling TryUpdateContext, and finally retrieved and passed to Dispose(T) exactly once when Dispose() is called.

When the base type invokes Dispose(T), your derived type should perform whatever disposing logic it needs to.

SingleNonblockingDisposable<T>

The SingleNonblockingDisposable<T> type is a base type for disposables that desire exactly-once semantics without blocking other threads calling Dispose. It works exactly like SingleDisposable<T>, except that once disposal has started, other threads calling Dispose will return immediately, treating the additional Dispose calls as a no-op.

About

IDisposable helper types.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%