IAsyncDisposable IAsyncDisposable IAsyncDisposable IAsyncDisposable Interface

定义

提供一种用于异步释放非托管资源的机制。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. 但是,在某些情况下它们需要提供一种异步机制用于释放非托管的资源,除 (或 instead of) 同步的一个。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.

结合使用异步方法asyncawait中的关键字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. 若要确保即使发生异常时释放资源,请调用DisposeAsync方法内的finally子句try / finally语句。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. 有关详细信息try / finally模式,请参阅try finally (C#) 或尝试...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.0For 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() DisposeAsync() DisposeAsync() DisposeAsync()

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

扩展方法

ConfigureAwait(IAsyncDisposable, Boolean) ConfigureAwait(IAsyncDisposable, Boolean) ConfigureAwait(IAsyncDisposable, Boolean) ConfigureAwait(IAsyncDisposable, Boolean)

Configures how awaits on the tasks returned from an async disposable are performed.

ConfigureAwait(IAsyncDisposable, Boolean) ConfigureAwait(IAsyncDisposable, Boolean) ConfigureAwait(IAsyncDisposable, Boolean) ConfigureAwait(IAsyncDisposable, Boolean)

适用于