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. 不過, 在某些情況下, 他們需要提供非同步機制來釋放非受控資源, 以及 (或不是) 同步。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. 為了確保即使發生例外狀況, 也會釋放資源, 請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() DisposeAsync() DisposeAsync() DisposeAsync()

非同步執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之由應用程式定義的工作。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)

設定如何執行從非同步可處置項目傳回的工作 await。Configures how awaits on the tasks returned from an async disposable are performed.

適用於