Do I need to Dispose() or Close() an EventWaitHandle?

前端 未结 4 670
借酒劲吻你
借酒劲吻你 2020-12-28 15:35

If I am using EventWaitHandle (or AutoResetEvent, ManualResetEvent) to synchronise between threads then do I need to call the Cl

4条回答
  •  北海茫月
    2020-12-28 16:03

    The disposable resource of an EventWaitHandle is actually a SafeHandle (wrapped in a SafeWaitHandle). SafeHandle implements a finalizer, which eventually makes sure the necessary resource is release, so it should be safe to let the garbage collector / finalizer thread handle it in this case.

    However, it is always a good idea to explicitly call Dispose() when the resource is no longer needed.

    The threading chapter in C# 3.0 in a Nutshell states

    This practice is (arguably) acceptable with wait handles because they have a light OS burden (asynchronous delegates rely on exactly this mechanism to release their IAsyncResult's wait handle).

提交回复
热议问题