tags:

views:

34

answers:

2

System.Threading.WaitHandle (and hence ManualResetEvent etc) implement IDisposable explicitly. This seems to discourage it's use (calling Close() achieves the same as casting to IDisposable and calling Dispose).

I'm just wondering if there is a good reason for this?

A: 

Because calling Close is the preferred way of closing these kinds of system resources.

Eric
+1  A: 

There's awkwardness in the .NET APIs induced by Dispose(). Other examples of classes that have both a Close and a Dispose method are the Stream derived classes, Socket, TcpClient, EventLog, Process, SqlConnection and many others.

I don't know any example of such a class that doesn't simply call Dispose() in its Close() method implementation. In other words, you're always fine using the "using" statement and not calling Close. The API designers however preferred keeping Close() around as a more literal way of ending the use of the object. That's defensible, the Dispose() method tends to be inherited from a base class, often a very obscure one.

Hans Passant
precisely - it's because of the developers' choice of the public interface of the type in question. Dispose is, let's face it, a bit smelly; and in the case of a handle (especially for anyone that's done Win32 API work in C or C++ for example), 'Close' makes so much more sense. It's also shorter ;)
Andras Zoltan