IAsyncDisposable.DisposeAsync Method

Definition

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public:
 System::Threading::Tasks::ValueTask DisposeAsync();
public System.Threading.Tasks.ValueTask DisposeAsync ();
abstract member DisposeAsync : unit -> System.Threading.Tasks.ValueTask
Public Function DisposeAsync () As ValueTask

Returns

A task that represents the asynchronous dispose operation.

Remarks

Use this method to asynchronously close or release unmanaged resources such as files, streams, and handles held by an instance of the class that implements this interface. Using this method instead of IDisposable.Dispose enables you to perform a resource-intensive dispose operation without blocking the main thread of a GUI application for a long time.

Warning

If you are using a class that implements the IAsyncDisposable interface, you should call its DisposeAsync implementation when you are finished using the class. For more information, see the "Using an object that implements IAsyncDisposable" section in the IAsyncDisposable topic.

When implementing this method, ensure that all held resources are freed by propagating the call through the containment hierarchy. For example, if an object A allocates an object B, and object B allocates an object C, then A's DisposeAsync implementation must call DisposeAsync on B, which must in turn call DisposeAsync on C. An object must also call the DisposeAsync method of its base class if the base class implements IAsyncDisposable.

If an object's DisposeAsync method is called more than once, the object must ignore all calls after the first one and synchronously return a successfully completed ValueTask. The object must not throw an exception if its DisposeAsync method is called multiple times. Instance methods other than DisposeAsync can throw an ObjectDisposedException when resources are already disposed.

Applies to