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.

備註

您應該針對指定true , 以disposing釋放所有資源。You should release all resources by specifying true for disposing. disposingtrue時, 資料流程也可以確保資料會排清到基礎緩衝區, 並存取其他可終結的物件。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. DisposeDispose 用受true保護的方法, 並將參數設定為。disposingDispose invokes the protected Dispose method with the disposing parameter set to true. Finalize使用設定disposingfalse的叫用。 DisposeFinalize 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.

適用於