CriticalHandle.ReleaseHandle 方法


在衍生類別中覆寫時,執行釋放控制代碼所需的程式碼。When overridden in a derived class, executes the code required to free the handle.

 abstract bool ReleaseHandle();
protected abstract bool ReleaseHandle ();
abstract member ReleaseHandle : unit -> bool
Protected MustOverride Function ReleaseHandle () As Boolean


如果成功釋放控制代碼,則為 true;如果發生嚴重失敗的事件,則為 falsetrue if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. 在這種情況下,它會產生 releaseHandleFailed Managed 偵錯助理。In this case, it generates a releaseHandleFailed Managed Debugging Assistant.


只有在您採用適當的同步處理機制來確保只呼叫一次 CloseDispose 方法時,才會保證 ReleaseHandle 方法只被呼叫一次。The ReleaseHandle method is guaranteed to be called only once, provided that you employ proper synchronization mechanisms to ensure that only one call to the Close or Dispose method is made. 如果 trueIsInvalidIsClosed 屬性,則不會呼叫 ReleaseHandle 方法。The ReleaseHandle method will not be called if the IsInvalid or IsClosed property is true. 在您的 CriticalHandle 衍生類別中執行此方法,以執行釋放控制碼所需的任何程式碼。Implement this method in your CriticalHandle derived classes to execute any code that is required to free the handle. 因為 CriticalHandle 的其中一項功能是要確保防止資源流失,所以您的 ReleaseHandle 執行中的程式碼永遠不會失敗。Because one of the functions of CriticalHandle is to guarantee prevention of resource leaks, the code in your implementation of ReleaseHandle must never fail. 垃圾收集行程會在針對同時進行垃圾收集的物件執行一般完成項之後,呼叫 ReleaseHandle,並保證資源叫用,而且在進行中時不會中斷。The garbage collector calls ReleaseHandle after normal finalizers have been run for objects that were garbage collected at the same time, and guarantees the resources to invoke it and that it will not be interrupted while it is in progress. 這個方法會在實例結構時間做為條件約束的執列區域(CER)準備(以及其靜態可視為呼叫圖形中的所有方法)。This method will be prepared as a constrained execution region (CER) at instance construction time (along with all the methods in its statically determinable call graph). 雖然這可避免執行緒中止中斷,但您仍然必須小心不要在覆寫的 ReleaseHandle 方法中引進任何錯誤路徑。Although this prevents thread abort interrupts, you must still be careful not to introduce any fault paths in your overridden ReleaseHandle method. 特別的是,將 ReliabilityContractAttribute 屬性套用至您從 ReleaseHandle呼叫的任何方法。In particular, apply the ReliabilityContractAttribute attribute to any methods you call from ReleaseHandle. 在大部分情況下,此程式碼應該是:In most cases this code should be:

ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)

此外,若要進行簡單清除(例如,在檔案控制代碼上呼叫 Windows API CloseHandle),您可以檢查單一平台叫用呼叫的傳回值。Additionally, for simple cleanup (for example, calling the Windows API CloseHandle on a file handle) you can check the return value for the single platform invoke call. 針對複雜的清除,您可能會有許多程式邏輯和許多方法呼叫,其中有些可能會失敗。For complex cleanup, you may have a lot of program logic and many method calls, some of which might fail. 您必須確定您的程式邏輯具有每個案例的 fallback 碼。You must ensure that your program logic has fallback code for each of those cases.

如果 ReleaseHandle 方法因為任何原因而傳回 false,它會產生releaseHandleFailed Managed 偵錯工具。If the ReleaseHandle method returns false for any reason, it generates a releaseHandleFailed Managed Debugging Assistant.