SafeHandle.Close Method


标记句柄,以便释放资源。Marks the handle for releasing and freeing resources.

 void Close();
public void Close ();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()


调用 CloseDispose 方法可释放资源。Calling the Close or Dispose method allows the resources to be freed. 如果其他线程使用相同的安全句柄对象,则这可能不会立即发生,但一旦不再出现,就会发生这种情况。This might not happen immediately if other threads are using the same safe handle object, but will happen as soon as that is no longer the case. 尽管大多数使用 SafeHandle 类的类不需要提供终结器,但有时需要这样做(例如,刷新文件缓冲区或将某些数据写回内存中)。Although most classes that use the SafeHandle class do not need to provide a finalizer, this is sometimes necessary (for example, to flush out file buffers or to write some data back into memory). 在这种情况下,类可以提供一个终结器,保证在运行 SafeHandle 关键终结器之前运行。In this case, the class can provide a finalizer that is guaranteed to run before the SafeHandle critical finalizer runs.

使用完 SafeHandle 对象后,调用 CloseDispose 方法。Call the Close or Dispose method when you are finished using the SafeHandle object.


在释放对 SafeHandle 对象的最后一个引用之前,始终调用 CloseDisposeAlways call Close or Dispose before you release your last reference to the SafeHandle object. 否则,在垃圾回收器调用 SafeHandle 对象的 Finalize 方法之前,该对象正在使用的资源不会被释放。Otherwise, the resources it is using will not be freed until the garbage collector calls the SafeHandle object's Finalize method.

Applies to