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.

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

异步方法与 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 # 的异步编程模型采用 Async 和 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. 若要确保在发生异常的情况下释放资源,请将使用对象的代码放 IAsyncDisposable 入 c # 中的using语句 (从版本8.0 开始) 或者在 DisposeAsync finally 语句的子句内调用方法 try / finallyTo make sure resources are released even in case of an exception, put the code that uses the IAsyncDisposable object into the using statement (in C# beginning from version 8.0) or call the DisposeAsync method inside a finally clause of the try/finally statement. 有关模式的详细信息 try / finally ,请参阅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. 有关详细信息,请参阅 实现 DisposeAsync 方法For more information, see Implement a DisposeAsync method.

方法

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.

适用于