Task.Dispose 方法

定義

釋放 Task 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the Task class.

多載

Dispose()

釋放 Task 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the Task class.

Dispose(Boolean)

處置 Task,並釋放它的所有 Unmanaged 資源。Disposes the Task, releasing all of its unmanaged resources.

Dispose()

釋放 Task 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the Task class.

public:
 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()

實作

例外狀況

工作不是下列其中一個最終狀態:RanToCompletionFaultedCanceledThe task is not in one of the final states: RanToCompletion, Faulted, or Canceled.

備註

Task 類別會執行 IDisposable 介面,因為內部會使用也會實作為 IDisposable的資源。The Task class implements the IDisposable interface because internally it uses resources that also implement IDisposable. 不過,特別是當您的應用程式以 .NET Framework 4.5.NET Framework 4.5 或更新版本為目標時,不需要呼叫 Dispose 除非效能或擴充性測試指出,根據您的使用模式,您的應用程式效能會藉由處置工作而獲得改善。However, particularly if your app targets the .NET Framework 4.5.NET Framework 4.5 or later, there is no need to call Dispose unless performance or scalability testing indicates that, based on your usage patterns, your app's performance would be improved by disposing of tasks. 如需詳細資訊,請參閱使用 .NET 進行平行程式設計中的「我需要處置工作嗎? 」。For more information, see Do I need to dispose of Tasks? in the Parallel Programming with .NET blog.

Dispose(Boolean)

處置 Task,並釋放它的所有 Unmanaged 資源。Disposes the Task, releasing all of its unmanaged resources.

protected:
 virtual void Dispose(bool disposing);
protected virtual void Dispose (bool disposing);
abstract member Dispose : bool -> unit
override this.Dispose : bool -> unit
Protected Overridable Sub Dispose (disposing As Boolean)

參數

disposing
Boolean

布林值,表示是否會因為呼叫 Dispose() 而呼叫這個方法。A Boolean value that indicates whether this method is being called due to a call to Dispose().

例外狀況

工作不是下列其中一個最終狀態:RanToCompletionFaultedCanceledThe task is not in one of the final states: RanToCompletion, Faulted, or Canceled.

備註

Task 類別會執行 IDisposable 介面,因為內部會使用也會實作為 IDisposable的資源。The Task class implements the IDisposable interface because internally it uses resources that also implement IDisposable. 不過,特別是當您的應用程式以 .NET Framework 4.5.NET Framework 4.5 或更新版本為目標時,不需要呼叫 Dispose 除非效能或擴充性測試指出,根據您的使用模式,您的應用程式效能會藉由處置工作而獲得改善。However, particularly if your app targets the .NET Framework 4.5.NET Framework 4.5 or later, there is no need to call Dispose unless performance or scalability testing indicates that, based on your usage patterns, your app's performance would be improved by disposing of tasks. 如需詳細資訊,請參閱使用 .NET 進行平行程式設計中的「我需要處置工作嗎? 」。For more information, see Do I need to dispose of Tasks? in the Parallel Programming with .NET blog.

執行緒安全性

不同于 Task 類別的大部分成員,這個方法不是安全線程。Unlike most of the members of the Task class, this method is not thread-safe.

適用於