If I am using EventWaitHandle
(or AutoResetEvent
, ManualResetEvent
) to synchronise between threads then do I need to call the Close()
or Dispose()
methods on that event handle when I am done with it?
如果我使用EventWaitHandle(或AutoResetEvent,ManualResetEvent)来在线程之间进行同步,那么当我完成它时,是否需要在该事件句柄上调用Close()或Dispose()方法?
EventWaitHandle
inherits from WaitHandle
, which implements IDisposable
. And FxCop complains if I don't implement IDisposable
on any class that contains an EventWaitHandle
. So this suggests that I do need to call it.
EventWaitHandle继承自WaitHandle,后者实现了IDisposable。如果我没有在任何包含EventWaitHandle的类上实现IDisposable,FxCop会抱怨。所以这表明我确实需要打电话给它。
However none of these MSDN usage examples call Dispose()
or Close()
:
但是,这些MSDN用法示例都没有调用Dispose()或Close():
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
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?
这只是微软无视自己建议的一个例子吗?
3 个解决方案
#1
21
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.
EventWaitHandle的可支配资源实际上是一个SafeHandle(包装在SafeWaitHandle中)。 SafeHandle实现了一个终结器,它最终确保释放必要的资源,因此在这种情况下让垃圾收集器/终结器线程处理它应该是安全的。
However, it is always a good idea to explicitly call Dispose()
when the resource is no longer needed.
但是,在不再需要资源时显式调用Dispose()总是一个好主意。
The threading chapter in C# 3.0 in a Nutshell states
Cuts 3.0中的线程章节在Nutshell中说明
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).这种做法(可以说)可以接受等待句柄,因为它们具有轻微的操作系统负担(异步委托完全依赖于这种机制来释放它们的IAsyncResult的等待句柄)。
#2
6
You need to dispose them explicitly. Close() is more appropriate for them as it does call Dispose().
您需要明确处理它们。 Close()更适合它们,因为它调用Dispose()。
#3
2
Class definitions from MSDN:
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.
所以是的,你必须因为WaitHandle是IDisposable。如果不这样做,FxCop会发现这违反规则。
#1
21
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.
EventWaitHandle的可支配资源实际上是一个SafeHandle(包装在SafeWaitHandle中)。 SafeHandle实现了一个终结器,它最终确保释放必要的资源,因此在这种情况下让垃圾收集器/终结器线程处理它应该是安全的。
However, it is always a good idea to explicitly call Dispose()
when the resource is no longer needed.
但是,在不再需要资源时显式调用Dispose()总是一个好主意。
The threading chapter in C# 3.0 in a Nutshell states
Cuts 3.0中的线程章节在Nutshell中说明
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).这种做法(可以说)可以接受等待句柄,因为它们具有轻微的操作系统负担(异步委托完全依赖于这种机制来释放它们的IAsyncResult的等待句柄)。
#2
6
You need to dispose them explicitly. Close() is more appropriate for them as it does call Dispose().
您需要明确处理它们。 Close()更适合它们,因为它调用Dispose()。
#3
2
Class definitions from MSDN:
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.
所以是的,你必须因为WaitHandle是IDisposable。如果不这样做,FxCop会发现这违反规则。