Class ObjectPool<T>
- Namespace
- CodeProject.ObjectPool
- Assembly
- CodeProject.ObjectPool.dll
Generic object pool.
public class ObjectPool<T> : IObjectPool<T> where T : PooledObject
Type Parameters
T
The type of the object that which will be managed by the pool. The pooled object have to be a sub-class of PooledObject.
- Inheritance
-
ObjectPool<T>
- Implements
-
IObjectPool<T>
- Derived
- Inherited Members
Constructors
ObjectPool()
Initializes a new pool with default settings.
public ObjectPool()
ObjectPool(EvictionSettings)
Initializes a new pool with specified eviction settings.
public ObjectPool(EvictionSettings evictionSettings)
Parameters
evictionSettings
EvictionSettingsSettings for the validation and eviction job.
ObjectPool(Func<CancellationToken, Task<T>>?)
Initializes a new pool with specified factory method.
public ObjectPool(Func<CancellationToken, Task<T>>? asyncFactoryMethod)
Parameters
asyncFactoryMethod
Func<CancellationToken, Task<T>>The async factory method that will be used to create new objects.
ObjectPool(Func<T>?)
Initializes a new pool with specified factory method.
public ObjectPool(Func<T>? factoryMethod)
Parameters
factoryMethod
Func<T>The factory method that will be used to create new objects.
ObjectPool(int)
Initializes a new pool with specified maximum pool size.
public ObjectPool(int maximumPoolSize)
Parameters
maximumPoolSize
intThe maximum pool size limit.
Exceptions
- ArgumentOutOfRangeException
maximumPoolSize
is less than or equal to zero.
ObjectPool(int, Func<CancellationToken, Task<T>>?)
Initializes a new pool with specified factory method and maximum size.
public ObjectPool(int maximumPoolSize, Func<CancellationToken, Task<T>>? asyncFactoryMethod)
Parameters
maximumPoolSize
intThe maximum pool size limit.
asyncFactoryMethod
Func<CancellationToken, Task<T>>The async factory method that will be used to create new objects.
Exceptions
- ArgumentOutOfRangeException
maximumPoolSize
is less than or equal to zero.
ObjectPool(int, Func<CancellationToken, Task<T>>?, EvictionSettings, IEvictionTimer?)
Initializes a new pool with specified factory method, maximum size, eviction timer and settings.
public ObjectPool(int maximumPoolSize, Func<CancellationToken, Task<T>>? asyncFactoryMethod, EvictionSettings evictionSettings, IEvictionTimer? evictionTimer)
Parameters
maximumPoolSize
intThe maximum pool size limit.
asyncFactoryMethod
Func<CancellationToken, Task<T>>The async factory method that will be used to create new objects.
evictionSettings
EvictionSettingsSettings for the validation and eviction job.
evictionTimer
IEvictionTimerThe eviction timer used to schedule an async validation and eviction job.
Exceptions
- ArgumentOutOfRangeException
maximumPoolSize
is less than or equal to zero.
ObjectPool(int, Func<T>?)
Initializes a new pool with specified factory method and maximum size.
public ObjectPool(int maximumPoolSize, Func<T>? factoryMethod)
Parameters
maximumPoolSize
intThe maximum pool size limit.
factoryMethod
Func<T>The factory method that will be used to create new objects.
Exceptions
- ArgumentOutOfRangeException
maximumPoolSize
is less than or equal to zero.
ObjectPool(int, Func<T>?, EvictionSettings?, IEvictionTimer?)
Initializes a new pool with specified factory method, maximum size, eviction timer and settings.
public ObjectPool(int maximumPoolSize, Func<T>? factoryMethod, EvictionSettings? evictionSettings, IEvictionTimer? evictionTimer)
Parameters
maximumPoolSize
intThe maximum pool size limit.
factoryMethod
Func<T>The factory method that will be used to create new objects.
evictionSettings
EvictionSettingsSettings for the validation and eviction job.
evictionTimer
IEvictionTimerThe eviction timer used to schedule an async validation and eviction job.
Exceptions
- ArgumentOutOfRangeException
maximumPoolSize
is less than or equal to zero.
Properties
AsyncFactoryMethod
public Func<CancellationToken, Task<T>> AsyncFactoryMethod { get; protected set; }
Property Value
- Func<CancellationToken, Task<T>>
Diagnostics
Gets the Diagnostics class for the current Object Pool, whose goal is to record data about how the pool operates. By default, however, an object pool records anything; you have to enable it through the Enabled property.
public ObjectPoolDiagnostics Diagnostics { get; set; }
Property Value
FactoryMethod
Gets the Factory method that will be used for creating new objects.
public Func<T> FactoryMethod { get; protected set; }
Property Value
- Func<T>
MaximumPoolSize
Gets or sets the maximum number of objects that could be available at the same time in the pool.
public int MaximumPoolSize { get; set; }
Property Value
ObjectsInPoolCount
Gets the count of the objects currently in the pool.
public int ObjectsInPoolCount { get; }
Property Value
PooledObjects
The concurrent buffer containing pooled objects.
protected PooledObjectBuffer<T> PooledObjects { get; }
Property Value
Methods
Clear()
Clears the pool and destroys each object stored inside it.
public void Clear()
CreatePooledObject()
Creates a new pooled object, initializing its info.
protected virtual T CreatePooledObject()
Returns
- T
A new pooled object.
CreatePooledObjectAsync(CancellationToken)
Creates a new pooled object, initializing its info.
protected virtual Task<T> CreatePooledObjectAsync(CancellationToken cancellationToken)
Parameters
cancellationToken
CancellationTokenCancellation token
Returns
- Task<T>
A new pooled object.
DestroyPooledObject(PooledObject)
Destroys given pooled object, disposing its resources.
protected void DestroyPooledObject(PooledObject objectToDestroy)
Parameters
objectToDestroy
PooledObjectThe pooled object that should be destroyed.
Exceptions
- ArgumentNullException
objectToDestroy
isnull
.
~ObjectPool()
ObjectPool destructor.
protected ~ObjectPool()
GetObject()
Gets a monitored object from the pool.
public T GetObject()
Returns
- T
A monitored object from the pool.
Exceptions
- InvalidOperationException
If a custom async factory method has been specified, this exception is thrown in order not to perform a sync-over- async operation, which might lead to deadlocks.
GetObjectAsync(CancellationToken)
Gets a monitored object from the pool.
public Task<T> GetObjectAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenCancellation token
Returns
- Task<T>
A monitored object from the pool.
StartEvictor(EvictionSettings)
Starts the evictor process, if enabled.
protected void StartEvictor(EvictionSettings settings)
Parameters
settings
EvictionSettingsEviction settings.