PeanutButter
Loading...
Searching...
No Matches
PeanutButter.Utils.AutoDisposer Class Reference

Provides a mechanism to dispose of other disposables when it is disposed. This allows for flattening out nested using() blocks with an outer AutoDisposer which takes care of disposing registered items (in reverse order) when it is disposed. More...

Inheritance diagram for PeanutButter.Utils.AutoDisposer:

Public Member Functions

 AutoDisposer (params IDisposable[] toDispose)
 Constructs a new AutoDisposer.
void Add (params IDisposable[] toDispose)
 Adds zero or more IDisposable objects to the list to be disposed when this AutoDisposer is disposed.
 AutoDisposer (Action< IDisposable > beforeDisposing)
 Constructs a new AutoDisposer with an action to run, per-item, before disposing items.
Add< T > (T toDispose)
 Adds a single IDisposable object to the disposable list and returns that object. Use this to make your code flow better, eg: var someDisposable = autoDisposer.Add(new SomeDisposable());.
void Dispose ()
void DisposeNow (IDisposable disposable)
 Dispose of the item right now, rather than later.

Properties

bool ThreadedDisposal = false [get, set]
 When enabled, will background & parallelize disposal.
int MaxDegreeOfParallelism = Environment.ProcessorCount [get, set]
 When ThreadedDisposal is enabled, this determines how many threads to run in parallel for disposing.
bool BackgroundDisposal = false [get, set]
 When enabled, disposal happens in the background instead of halting the caller on the .Dispose() line.

Detailed Description

Provides a mechanism to dispose of other disposables when it is disposed. This allows for flattening out nested using() blocks with an outer AutoDisposer which takes care of disposing registered items (in reverse order) when it is disposed.

Constructor & Destructor Documentation

◆ AutoDisposer() [1/2]

PeanutButter.Utils.AutoDisposer.AutoDisposer ( params IDisposable[] toDispose)

Constructs a new AutoDisposer.

Parameters
toDisposeParams array of objects implementing IDisposable which the AutoDisposer will dispose of when it it disposed

◆ AutoDisposer() [2/2]

PeanutButter.Utils.AutoDisposer.AutoDisposer ( Action< IDisposable > beforeDisposing)

Constructs a new AutoDisposer with an action to run, per-item, before disposing items.

Parameters
beforeDisposing

Member Function Documentation

◆ Add()

void PeanutButter.Utils.AutoDisposer.Add ( params IDisposable[] toDispose)

Adds zero or more IDisposable objects to the list to be disposed when this AutoDisposer is disposed.

Parameters
toDisposeParams array of objects to watch. Objects are disposed in reverse order.

◆ Add< T >()

T PeanutButter.Utils.AutoDisposer.Add< T > ( T toDispose)

Adds a single IDisposable object to the disposable list and returns that object. Use this to make your code flow better, eg: var someDisposable = autoDisposer.Add(new SomeDisposable());.

Parameters
toDisposeIDisposable to dispose of at a later date
Template Parameters
TThe type of the IDisposable to add
Returns
The item added to the auto-disposing collection
Type Constraints
T :IDisposable 

◆ DisposeNow()

void PeanutButter.Utils.AutoDisposer.DisposeNow ( IDisposable disposable)

Dispose of the item right now, rather than later.

Parameters
disposable

The documentation for this class was generated from the following file:
  • source/Utils/PeanutButter.Utils/AutoDisposer.cs