SafeHandle.DangerousAddRef(Boolean) 方法


手動遞增 SafeHandle 執行個體上的參考計數器。Manually increments the reference counter on SafeHandle instances.

 void DangerousAddRef(bool % success);
public void DangerousAddRef (ref bool success);
public void DangerousAddRef (ref bool success);
member this.DangerousAddRef : bool -> unit
member this.DangerousAddRef : bool -> unit
Public Sub DangerousAddRef (ByRef success As Boolean)



如果成功遞增參考計數器,則為 true,否則為 falsetrue if the reference counter was successfully incremented; otherwise, false.



DangerousAddRef方法會防止 common language runtime 回收控制碼所使用的記憶體 (這會在執行時間呼叫 ReleaseHandle 方法) 時發生。The DangerousAddRef method prevents the common language runtime from reclaiming memory used by a handle (which occurs when the runtime calls the ReleaseHandle method). 您可以使用這個方法來手動遞增實例上的參考計數 SafeHandleYou can use this method to manually increment the reference count on a SafeHandle instance. DangerousAddRef 使用 ref () 的參數傳回布林值 success ,指出是否已成功遞增參考計數。DangerousAddRef returns a Boolean value using a ref parameter (success) that indicates whether the reference count was incremented successfully. 這可讓您的程式邏輯在發生失敗時進行備份。This allows your program logic to back out in case of failure. 您應該在 success 呼叫之前將設定為 false DangerousAddRefYou should set success to false before calling DangerousAddRef. 如果 success 為,則會比對 true 的呼叫與的對應呼叫,以避免資源流失 DangerousAddRef DangerousReleaseIf success is true, avoid resource leaks by matching the call to DangerousAddRef with a corresponding call to DangerousRelease.


這個方法適用于 advanced 使用者,必須一律謹慎使用。This method is intended for advanced users and must always be used carefully. 為了避免流失控制碼資源,請一律在受限制的執列區域內呼叫這個方法 (CER) ,其中線程中止無法中斷處理。To avoid leaking handle resources, always call this method inside a constrained execution region (CER), where a thread abort cannot interrupt processing.