CriticalHandle CriticalHandle CriticalHandle CriticalHandle Class


Represents a wrapper class for handle resources.

public ref class CriticalHandle abstract : System::Runtime::ConstrainedExecution::CriticalFinalizerObject, IDisposable
public abstract class CriticalHandle : System.Runtime.ConstrainedExecution.CriticalFinalizerObject, IDisposable
type CriticalHandle = class
    inherit CriticalFinalizerObject
    interface IDisposable
Public MustInherit Class CriticalHandle
Inherits CriticalFinalizerObject
Implements IDisposable


The CriticalHandle class is similar to the SafeHandle class, except that SafeHandle implements reference counting. You can use CriticalHandle instead of SafeHandle to address performance considerations when you can provide the necessary synchronization more efficiently yourself.

Because the CriticalHandle class does not perform reference counting, it does not provide protection from handle recycling security attacks. Because the reference counting algorithm implicitly serializes operations, a certain amount of thread safety is also lost. If you call the IDisposable.Dispose or Close method while an operation that is using the handle is outstanding on another thread, or if you call IDisposable.Dispose or Close from two threads at the same time, the results are non-deterministic. The CriticalHandle class still provides the guaranteed critical finalization provided by the CriticalFinalizerObject class.


CriticalHandle(IntPtr) CriticalHandle(IntPtr) CriticalHandle(IntPtr) CriticalHandle(IntPtr)

Initializes a new instance of the CriticalHandle class with the specified invalid handle value.


handle handle handle handle

Specifies the handle to be wrapped.


IsClosed IsClosed IsClosed IsClosed

Gets a value indicating whether the handle is closed.

IsInvalid IsInvalid IsInvalid IsInvalid

When overridden in a derived class, gets a value indicating whether the handle value is invalid.


Close() Close() Close() Close()

Marks the handle for releasing and freeing resources.

Dispose() Dispose() Dispose() Dispose()

Releases all resources used by the CriticalHandle.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases the unmanaged resources used by the CriticalHandle class specifying whether to perform a normal dispose operation.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

Frees all resources associated with the handle.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ReleaseHandle() ReleaseHandle() ReleaseHandle() ReleaseHandle()

When overridden in a derived class, executes the code required to free the handle.

SetHandle(IntPtr) SetHandle(IntPtr) SetHandle(IntPtr) SetHandle(IntPtr)

Sets the handle to the specified pre-existing handle.

SetHandleAsInvalid() SetHandleAsInvalid() SetHandleAsInvalid() SetHandleAsInvalid()

Marks a handle as invalid.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)


for full trust for inheritors. This member cannot be inherited by partially trusted code.

requires full trust for the immediate caller. This class cannot be used by partially trusted or transparent code.

Applies to

See Also