IAsyncDisposable 接口

定义

提供一种用于异步释放非托管资源的机制。Provides a mechanism for releasing unmanaged resources asynchronously.

public interface class IAsyncDisposable
public interface IAsyncDisposable
type IAsyncDisposable = interface
Public Interface IAsyncDisposable
派生

注解

在 .net 中, 拥有非托管资源的类通常IDisposable实现接口, 以提供一种同步释放非托管资源的机制。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. 提供此类机制使使用者能够在不会长时间地阻止 GUI 应用程序的主线程的情况下执行占用大量资源的释放操作。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.

此接口的ValueTask方法返回一个表示异步释放操作的。 IAsyncDisposable.DisposeAsyncThe 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.

异步方法与和中async await C#的和关键字结合使用, 并 Visual Basic。The async methods are used in conjunction with the async and await keywords in C# and Visual Basic. 有关更多 insformation, 请参阅中C#的任务异步编程模型或异步和 Await 的异步编程 (Visual Basic)For more insformation, see The Task asynchronous programming model in C# or Asynchronous Programming with Async and Await (Visual Basic).

使用实现 IAsyncDisposable 的对象Using an object that implements IAsyncDisposable

如果你的应用程序使用实现IAsyncDisposable的对象, 则应在使用完对象后调用该对象的DisposeAsync实现。If your application uses an object that implements IAsyncDisposable, you should call the object's DisposeAsync implementation when you are finished using it. 若要确保在发生异常的情况下释放资源, 请DisposeAsyncfinally语句的finally子句try /内调用方法。To make sure resources are released even in case of an exception, call the DisposeAsync method inside a finally clause of the try/finally statement. 有关finally模式的详细/信息try , 请参阅try-finally (C#) 或try .。。Catch .。。Finally 语句(Visual Basic)。For more information about the try/finally pattern, see try-finally (C#) or Try...Catch...Finally Statement (Visual Basic).

实现 IAsyncDisposableImplementing IAsyncDisposable

您可以在IAsyncDisposable以下情况下实现:You might implement IAsyncDisposable in the following situations:

  • 开发拥有非托管资源的异步枚举器时。When developing an asynchronous enumerator that owns unmanaged resources. 异步枚举器用于C# 8.0 异步流功能。Asynchronous enumerators are used with the C# 8.0 async streams feature. 有关异步流的详细信息, 请参阅教程:使用C# 8.0 和 .net Core 3.0生成和使用异步流。For more information about async streams, see Tutorial: Generate and consume async streams using C# 8.0 and .NET Core 3.0.

  • 如果你的类拥有非托管资源并释放它们, 则需要进行大量资源的 i/o 操作, 例如将中间缓冲区的内容刷新到文件中, 或通过网络发送数据包来关闭连接。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.

DisposeAsync使用方法可以在使用非托管资源 (例如释放、释放或重置非托管资源) 后执行任何所需的清理。Use the DisposeAsync method to perform whatever cleanup is necessary after using the unmanaged resources, such as freeing, releasing, or resetting the unmanaged resources. 有关 .NET 中的非托管资源的详细信息, 请参阅清理非托管资源For more information about unmanaged resources in .NET, see Cleaning Up Unmanaged Resources.

方法

DisposeAsync()

以异步方式执行与释放或重置非托管资源相关的应用程序定义的任务。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

扩展方法

ConfigureAwait(IAsyncDisposable, Boolean)

配置如何执行从异步可处置项返回的任务的等待。Configures how awaits on the tasks returned from an async disposable are performed.

适用于