Do I need to Dispose() or Close() an EventWaitHandle?
Solution 1
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).
Solution 2
You need to dispose them explicitly. Close() is more appropriate for them as it does call Dispose().
Solution 3
Class definitions from MSDN:
public class EventWaitHandle : WaitHandle
public abstract class WaitHandle : MarshalByRefObject, IDisposable
So yes you must as WaitHandle is IDisposable. FxCop would find this as a rule violation if you didn't.
Related videos on Youtube
django11
I'm a Senior Software Engineer, currently based near Newcastle (England) and working remotely for a company based in Fife (Scotland). I've been programming professionally since 1997 in the usual variety of languages (C, C++, C#, Java, Python, VB, HTML/CSS, etc) working on everything from low-level bit-bashing device drivers and microprocessor-based firmware for safety-critical medical devices, up to full Windows applications and enterprise systems. I hate cucumbers. If you're interested then here is my linkedin.com profile. SOreadytohelp
Updated on April 17, 2022Comments
-
django11 about 2 years
If I am using
EventWaitHandle
(orAutoResetEvent
,ManualResetEvent
) to synchronise between threads then do I need to call theClose()
orDispose()
methods on that event handle when I am done with it?EventWaitHandle
inherits fromWaitHandle
, which implementsIDisposable
. And FxCop complains if I don't implementIDisposable
on any class that contains anEventWaitHandle
. So this suggests that I do need to call it.However none of these MSDN usage examples call
Dispose()
orClose()
:http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle(VS.80).aspx http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent(VS.80).aspx http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(VS.80).aspx
Is this just an example of Microsoft ignoring their own advice?
-
django11 about 15 yearsThanks Peter, but why don't the MSDN usage examples call Close() or Dispose()? Is this just Microsoft not following their own advice?
-
Peter Drier about 15 yearsI'd guess it's 50% not following their own advice, and 50% trying to keep the examples as brief as possible.. with a side dish of the folks writing the examples (not the core sdk developers) not knowing better.
-
Dave Black over 9 yearsI'd argue that letting the GC collect it is quite undesirable. Leaving the work to the Finalizer thread is just bad practice. There is but a single finalizer thread and causing it to do more than it really should is not good. If the thread gets blocked, your app is hung. If an exception is thrown on the finalizer thread, your AppDomain crashes. Having the proper dispose pattern guarantees that GC.SuppressFinalize() is called thus suppressing finalization of this object. I have an example of implementing IDisposable - dave-black.blogspot.com/2011/03/…
-
Brian Rasmussen over 9 yearsI agree and that's why I state that you should explicitly call
Dispose
. -
Lunar Whisper over 5 years@PeterDrier The examples use static wait handles.