CriticalHandle.Close 方法

定義

標記要釋出和釋放資源的控制代碼。Marks the handle for releasing and freeing resources.

public:
 void Close();
public void Close ();
[System.Security.SecurityCritical]
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()
屬性

備註

呼叫 CloseDispose 方法,可讓資源釋放。Calling the Close or Dispose method allows the resources to be freed. 不同于 SafeHandle 類別,這一律會立即發生,因為沒有參考計數可指出其他執行緒正在使用這個控制碼。Unlike the SafeHandle class, this will always happen immediately since there is no reference count to indicate that other threads are using this handle. 因此,您必須使用同步處理機制,以確保可以安全地呼叫 Close 方法。Therefore, you must employ a synchronization mechanism to ensure it is safe to call the Close method. 雖然大部分使用 CriticalHandle 類別的類別都不需要提供完成項,但有時也需要這麼做(例如,清除檔案緩衝區,或將一些資料寫回記憶體中)。Although most classes that use the CriticalHandle 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). 在此情況下,類別可以提供在 CriticalHandle 重要完成項執行之前保證執行的完成項。In this case, the class can provide a finalizer that is guaranteed to run before the CriticalHandle critical finalizer runs.

當您完成使用 CriticalHandle 物件時,請呼叫 CloseDispose 方法。Call the Close or Dispose method when you are finished using the CriticalHandle object. Close 方法會使 CriticalHandle 物件處於無法使用的狀態。The Close method leaves the CriticalHandle object in an unusable state.

注意在您釋放 CriticalHandle 物件的最後一個參考之前,請一律呼叫 CloseDisposeNote Always call Close or Dispose before you release your last reference to the CriticalHandle object. 否則工作窗格所使用的資源不會釋放,直到記憶體回收行程呼叫 CriticalHandle 物件的 Finalize 方法。Otherwise, the resources it is using will not be freed until the garbage collector calls the CriticalHandle object's Finalize method.

適用於