Stream.Dispose Method

Definition

释放 Stream 对象使用的所有资源。Releases all resources used by the Stream object.

Overloads

Dispose()

释放 Stream 使用的所有资源。Releases all resources used by the Stream.

Dispose(Boolean)

释放 Stream 使用的非托管资源,并选择性地释放托管资源。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 ()

Implements

Remarks

此方法通过将任何更改写入后备存储并关闭流以释放资源来释放流。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.

Notes to Inheritors

将 stream 对象的所有清理逻辑置于 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 使用的非托管资源,并选择性地释放托管资源。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)

Parameters

disposing
Boolean

如果为 true,则托管及非托管资源都释放;如果为 false,则仅释放非托管资源。true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks

你应通过为 disposing指定 true 来释放所有资源。You should release all resources by specifying true for disposing. true``disposing 时,流还可以确保将数据刷新到基础缓冲区,并访问其他可终止对象。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.

此方法由 public 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.

Notes to Inheritors

在派生类中,不要重写 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() 时已释放的对象。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.

Applies to