CompositeAsyncDisposable
The CompositeAsyncDisposable
represents a group of IAsyncDisposable
instances that are disposed together, when the CompositeAsyncDisposable
is disposed.
Usage
Starting with a CompositeAsyncDisposable
instance:
var subscriptions = new CompositeAsyncDisposable();
New IAsyncDisposable
instances can be added using either of the following approaches:
// Each 'subscription' represents any IAsyncDisposable
IAsyncDisposable subscription1 = ...
IAsyncDisposable subscription2 = ...
IAsyncDisposable subscription3 = ...
IAsyncDisposable subscription4 = ...
// Explicitly add it to the composite
subscriptions.Add(subscription1);
// Add it via an extension method
subscription2.DisposeWith(subscriptions);
// Add more than one at a time
subscriptions.Add(subscription3, subscription4);
Disposal
CompositeAsyncDisposable
Implements IDisposable
and IAsyncDisposable
.
IDisposable
The group of IAsyncDisposable
instances are best disposed of via DisposeAsync()
but for the situation where they need to be disposed of via Dispose()
, CompositeAsyncDisposable
will:
Create a background task
Dispose of each item asynchronously
IAsyncDisposable
For any CompositeAsyncDisposable
, such as:
var subscriptions = new CompositeAsyncDisposable();
Calling:
await subscriptions.DisposeAsync();
Will result in iterating each IAsyncDisposable
and calling its' DisposeAsync()
method.
Last modified: 31 March 2024