Provides a mechanism for releasing unmanaged resources asynchronously.
public interface class IAsyncDisposable
public interface IAsyncDisposable
type IAsyncDisposable = interface
Public Interface IAsyncDisposable
In .NET, classes that own unmanaged resources usually implement the IDisposable interface to provide a mechanism for releasing unmanaged resources synchronously. However, in some cases they need to provide an asynchronous mechanism for releasing unmanaged resources in addition to (or instead of) the synchronous one. Providing such a mechanism enables the consumer to perform resource-intensive dispose operations without blocking the main thread of a GUI application for a long time.
The IAsyncDisposable.DisposeAsync method of this interface returns a ValueTask that represents the asynchronous dispose operation. Classes that own unmanaged resources implement this method, and the consumer of these classes calls this method on an object when it is no longer needed.
The async methods are used in conjunction with the
await keywords in C# and Visual Basic. For more insformation, see The Task asynchronous programming model in C# or Asynchronous Programming with Async and Await (Visual Basic).
Using an object that implements IAsyncDisposable
If your application uses an object that implements
IAsyncDisposable, you should call the object's DisposeAsync implementation when you are finished using it. To make sure resources are released even in case of an exception, call the DisposeAsync method inside a
finally clause of the
finally statement. For more information about the
finally pattern, see try-finally (C#) or Try...Catch...Finally Statement (Visual Basic).
You might implement
IAsyncDisposable in the following situations:
When developing an asynchronous enumerator that owns unmanaged resources. Asynchronous enumerators are used with the C# 8.0 async streams feature. For more information about async streams, see Tutorial: Generate and consume async streams using C# 8.0 and .NET Core 3.0.
When your class owns unmanaged resources and releasing them requires a resource-intensive I/O operation, such as flushing the contents of an intermediate buffer into a file or sending a packet over a network to close a connection.
Use the DisposeAsync method to perform whatever cleanup is necessary after using the unmanaged resources, such as freeing, releasing, or resetting the unmanaged resources. For more information about unmanaged resources in .NET, see Cleaning Up Unmanaged Resources.
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
Configures how awaits on the tasks returned from an async disposable are performed.