Stream.Dispose 方法

定義

釋放由 Stream 物件使用的所有資源。Releases all resources used by the Stream object.

多載

Dispose()

釋出 Stream 所使用的全部資源。Releases all resources used by the Stream.

Dispose(Boolean)

釋放 Stream 所使用的 unmanaged 資源並選擇性地釋放 managed 的資源。Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

Dispose()

釋出 Stream 所使用的全部資源。Releases all resources used by the Stream.

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

實作

備註

這個方法會將任何變更寫入至備份存放區,並關閉資料流程以釋放資源,藉此處置資料流程。This method disposes the stream, by writing any changes to the backing store and closing the stream to release resources.

呼叫 Dispose 可讓 Stream 所使用的資源重新配置供其他用途之用。Calling Dispose allows the resources used by the Stream to be reallocated for other purposes. 如需 Dispose的詳細資訊,請參閱清除非受控資源For more information about Dispose, see Cleaning Up Unmanaged Resources.

給繼承者的注意事項

將串流物件的所有清除邏輯放在 Dispose(Boolean)中。Place all cleanup logic for your stream object in Dispose(Boolean). 不要覆寫 Close()Do not override Close().

請注意,由於回溯相容性需求的不同,此方法的實作為處置模式建議的指引。Note that because of backward compatibility requirements, this method's implementation differs from the recommended guidance for the Dispose pattern. 這個方法會呼叫 Close(),然後再呼叫 Dispose(Boolean)This method calls Close(), which then calls Dispose(Boolean).

Dispose(Boolean)

釋放 Stream 所使用的 unmanaged 資源並選擇性地釋放 managed 的資源。Releases the unmanaged resources used by the Stream and optionally releases the managed 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

true 表示釋放 Managed 和 Unmanaged 資源,false 則表示只釋放 Unmanaged 資源。true to release both managed and unmanaged resources; false to release only unmanaged resources.

備註

您應該藉由指定 disposingtrue 來釋放所有資源。You should release all resources by specifying true for disposing. disposing true時,資料流程也可以確保資料會排清到基礎緩衝區,並存取其他可終結的物件。When disposing is true, the stream can also ensure data is flushed to the underlying buffer, and access other finalizable objects. 這可能無法在從完成項中呼叫,因為在完成項之間沒有排序。This may not be possible when called from a finalizer due a lack of ordering among finalizers.

如果您的資料流程使用作業系統控制碼與其來源通訊,請考慮使用 SafeHandle 的子類別來進行此目的。If your stream is using an operating system handle to communicate with its source, consider using a subclass of SafeHandle for this purpose.

這個方法是由公用 Dispose 方法和 Finalize 方法所呼叫。This method is called by the public Dispose method and the Finalize method. Dispose 會叫用受保護的 Dispose 方法,並將 disposing 參數設定為 trueDispose invokes the protected Dispose method with the disposing parameter set to true. Finalize 會叫用 disposing 設定為 falseDisposeFinalize invokes Dispose with disposing set to false.

給繼承者的注意事項

在衍生類別中,請勿覆寫 Close() 方法,而是將所有資料流程清除邏輯放在 Dispose(Boolean) 方法中。In derived classes, do not override the Close() method, instead, put all of the Stream cleanup logic in the Dispose(Boolean) method.

Dispose() 可以被其他物件呼叫多次。Dispose() can be called multiple times by other objects. 覆寫 Dispose(Boolean) 時,請小心不要參考到先前已在對 Dispose() 的早期呼叫中被處置 (Dispose) 的物件。When overriding Dispose(Boolean), be careful not to reference objects that have been previously disposed of in an earlier call to Dispose(). 如需如何執行 Dispose(Boolean)的詳細資訊,請參閱執行 Dispose 方法For more information about how to implement Dispose(Boolean), see Implementing a Dispose Method.

如需 Dispose()Finalize()的詳細資訊,請參閱清除非受控資源For more information about Dispose() and Finalize(), see Cleaning Up Unmanaged Resources.

適用於