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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:16:10  来源:igfitidea点击:

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

c#.netmultithreadingsynchronization

提问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()方法?

EventWaitHandleinherits from WaitHandle, which implements IDisposable. And FxCop complains if I don't implement IDisposableon 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 EventWaitHandleis actually a SafeHandle(wrapped in a SafeWaitHandle). SafeHandleimplements 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 会认为这是违反规则的。