SharedDisposable<T> Class
- Namespace
- Kampute.HttpClient.Utilities
- Assembly
- Kampute.HttpClient.dll
Definition
Shares one instance of a disposable resource among its users, and disposes it when the last of them releases it.
public sealed class SharedDisposable<T>
where T : class, IDisposable- Inheritance
- object
- SharedDisposable<T>
Type Parameters
T- The type of the resource.
Remarks
Each user acquires a SharedDisposable<T>.Reference and disposes it when done. The resource is created when the first reference is acquired, and disposed when the last reference is disposed; a later reference creates a new instance. The class is thread-safe.
Constructors
SharedDisposable<T>()
Initializes a new instance of the SharedDisposable<T> class that creates the resource with the parameterless constructor of
T.public SharedDisposable<T>()SharedDisposable<T>(Func<T>)
Initializes a new instance of the SharedDisposable<T> class that creates the resource with a function.
public SharedDisposable<T>(Func<T> factory)Parameters
factoryFunc<T>- The function that creates the resource.
Exceptions
- ArgumentNullException
- Thrown if
factoryisnull.
Properties
ReferenceCount
Gets the number of references that are not yet disposed.
public int ReferenceCount { get; }Property Value
- int
- The number of active references.
Methods
AcquireReference()
Acquires a reference to the resource, creating the resource if no other reference is active.
public SharedDisposable<T>.Reference AcquireReference()Returns
- SharedDisposable<T>.Reference
- A new SharedDisposable<T>.Reference, which releases the resource when it is disposed.
