C# 我是否需要 Dispose() 或 Close() EventWaitHandle?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/659840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to Dispose() or Close() an EventWaitHandle?
提问by GrahamS
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
. 如果我不在IDisposable
任何包含EventWaitHandle
. 所以这表明我确实需要调用它。
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).aspxhttp://msdn.microsoft.com/en-us/library/system.threading.manualresetevent(VS.80).aspxhttp://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?
这只是微软无视自己建议的一个例子吗?
采纳答案by Brian Rasmussen
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.
an 的一次性资源EventWaitHandle
实际上是 a SafeHandle
(包裹在 a 中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 Nutshellstates
在穿线章果壳中的C#3.0的状态
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
的等待句柄)。
回答by Mohit Chakraborty
You need to dispose them explicitly. Close() is more appropriate for them as it does call Dispose().
您需要明确地处理它们。Close() 更适合他们,因为它确实调用了 Dispose()。
回答by Peter Drier
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 会认为这是违反规则的。