Class CompositeDisposableAsync
- Namespace
- ReactiveUI.Extensions.Async.Disposables
- Assembly
- ReactiveUI.Extensions.dll
Represents a thread-safe collection of asynchronous disposable objects that are disposed together as a group. Provides methods to add, remove, and asynchronously dispose contained resources as a single operation.
public sealed class CompositeDisposableAsync : IAsyncDisposable
- Inheritance
-
CompositeDisposableAsync
- Implements
- Extension Methods
Remarks
Use this class to manage the lifetime of multiple IAsyncDisposable resources, ensuring that all are disposed when the collection is disposed. Once disposed, the collection cannot be used to add or remove items. This class is not read-only and is safe for concurrent access from multiple threads.
Constructors
CompositeDisposableAsync()
Initializes a new instance of the CompositeDisposableAsync class.
public CompositeDisposableAsync()
CompositeDisposableAsync(IEnumerable<IAsyncDisposable>)
Initializes a new instance of the CompositeDisposableAsync class that contains the specified asynchronous. disposables.
public CompositeDisposableAsync(IEnumerable<IAsyncDisposable> disposables)
Parameters
disposablesIEnumerable<IAsyncDisposable>The collection of IAsyncDisposable instances to include in the composite. Cannot be null.
Remarks
Each disposable in the collection will be disposed asynchronously when the composite is disposed. The order in which disposables are disposed is the same as the order in the provided collection.
CompositeDisposableAsync(params IAsyncDisposable[])
Initializes a new instance of the CompositeDisposableAsync class that contains the specified asynchronous. disposables.
public CompositeDisposableAsync(params IAsyncDisposable[] disposables)
Parameters
disposablesIAsyncDisposable[]An array of objects that implement IAsyncDisposable to be managed by the composite. Cannot be null, but may be empty.
Remarks
Each disposable provided will be disposed asynchronously when the composite is disposed. The order in which disposables are disposed is the same as the order in the array.
CompositeDisposableAsync(int)
Initializes a new instance of the CompositeDisposableAsync class with the specified initial capacity.
public CompositeDisposableAsync(int capacity)
Parameters
capacityintThe number of elements that the collection can initially store. Must be greater than or equal to 0.
Exceptions
- ArgumentOutOfRangeException
Thrown when capacity is less than 0.
Properties
Count
Gets the number of elements contained in the collection.
public int Count { get; }
Property Value
IsDisposed
Gets a value indicating whether the object has been disposed.
public bool IsDisposed { get; }
Property Value
Methods
AddAsync(IAsyncDisposable)
Adds an asynchronous disposable item to the collection, or disposes it immediately if the collection has already been disposed.
public ValueTask AddAsync(IAsyncDisposable item)
Parameters
itemIAsyncDisposableThe item to add. The item must implement IAsyncDisposable and will be disposed asynchronously if the collection is disposed.
Returns
- ValueTask
A ValueTask that represents the asynchronous operation. The returned task is completed if the item was added; otherwise, it represents the asynchronous disposal of the item.
Clear()
Asynchronously disposes all items in the collection and removes them.
public ValueTask Clear()
Returns
- ValueTask
A task that represents the asynchronous clear operation.
Remarks
If the collection is already empty or has been disposed, this method performs no action. Each item is disposed asynchronously before being removed from the collection. This method is thread-safe.
Contains(IAsyncDisposable)
Determines whether the collection contains the specified asynchronous disposable item.
public bool Contains(IAsyncDisposable item)
Parameters
itemIAsyncDisposableThe asynchronous disposable item to locate in the collection. Can be null.
Returns
- bool
true if the specified item is found in the collection and the collection has not been disposed; otherwise, false.
Remarks
If the collection has been disposed, this method always returns false.
CopyTo(IAsyncDisposable[], int)
Copies the elements of the collection to the specified array, starting at the given array index.
public void CopyTo(IAsyncDisposable[] array, int arrayIndex)
Parameters
arrayIAsyncDisposable[]The one-dimensional array of IAsyncDisposable elements that is the destination of the elements copied from the collection. The array must have zero-based indexing.
arrayIndexintThe zero-based index in the destination array at which copying begins. Must be non-negative and less than the length of the array.
Exceptions
- ArgumentOutOfRangeException
Thrown when arrayIndex is less than zero, greater than or equal to the length of array, or when there is not enough space from arrayIndex to the end of array to accommodate all elements in the collection.
DisposeAsync()
Asynchronously releases all resources used by the collection and disposes of each contained asynchronous disposable object.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
Remarks
After calling this method, the collection is considered disposed and cannot be used. This method is thread-safe and can be called multiple times; subsequent calls after the first have no effect.
GetEnumerator()
Returns an enumerator that iterates through a snapshot of the collection and clears its contents.
public IEnumerator<IAsyncDisposable> GetEnumerator()
Returns
- IEnumerator<IAsyncDisposable>
An enumerator for the collection of IAsyncDisposable items present at the time of enumeration.
Remarks
The enumerator operates on a snapshot of the collection taken at the time of the call. After enumeration, the original collection is cleared. This method is thread-safe.
Remove(IAsyncDisposable)
Removes the specified item from the collection and disposes it asynchronously.
public ValueTask<bool> Remove(IAsyncDisposable item)
Parameters
itemIAsyncDisposableThe item to remove and dispose. Cannot be null.
Returns
- ValueTask<bool>
A task that represents the asynchronous remove operation. The task result is true if the item was found and removed; otherwise, false.
Remarks
If the item is not found in the collection, it is not disposed. This method is thread-safe.