multithreading - 为什么 AutoResetEvent 和 ManualResetEvent 不支持构造函数中的名称?

标签 multithreading .net-2.0 synchronization

在 .NET Framework 2.0 上,AutoResetEvent 和 ManualResetEvent 继承自 EventWaitHandle。 EventWaitHandle 类有 4 个不同的构造函数。 3 个构造函数支持为事件命名。另一方面,ManualResetEvent 和 AutoResetEvent 都不支持命名,并且提供接收初始状态的单个构造函数。我可以简单地继承 EventWaitHandle 并编写自己的支持所有构造函数重载的类的实现,但如果不需要,我不喜欢重新发明轮子。我的问题是:

  • 命名事件有什么特殊问题吗?
  • 您知道 Microsoft 为何不支持它吗?
  • 您是否有比继承 EventWaitHandle 类并调用适当的构造函数更好的建议(如下例所示)?
    public class MyAutoResetEvent: EventWaitHandle  
    {  
        public MyAutoResetEvent(bool initialState)  
            : base(initialState, EventResetMode.AutoReset)  
        {  
        }  
        public MyAutoResetEvent(bool initialState, string name)  
            : base(initialState, EventResetMode.AutoReset, name)  
        {  
        }  
        public MyAutoResetEvent(bool initialState, string name, out bool createdNew)  
            : base(initialState, EventResetMode.AutoReset, name, out createdNew)  
        {  
        }  
        public MyAutoResetEvent(bool initialState, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity)  
            : base(initialState, EventResetMode.AutoReset, string.Empty, out createdNew, eventSecurity)  
        {  
        }  
    }  

最佳答案

您可以像这样创建一个命名的手动重置事件:

// Open the event by name.
EventWaitHandle namedMRSE = 
    new EventWaitHandle(false, EventResetMode.ManualReset, @"TheName");

Here is the reference对于上面的代码。我不知道设计背后的具体原因,但是there are some notes在 msdn 上,建议根据应用程序域和进程进行区分:

Event wait handles are useful in many of the same synchronization scenarios as the Monitor class. Event wait handles are often easier to use than the System.Threading.Monitor.Wait and System.Threading.Monitor.Pulse(System.Object) methods, and they offer more control over signaling. Named event wait handles can also be used to synchronize activities across application domains and processes, whereas monitors are local to an application domain.

关于multithreading - 为什么 AutoResetEvent 和 ManualResetEvent 不支持构造函数中的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2816759/

相关文章:

java - 以下两种方法来调度和停止线程哪一个更好

c# - SQL 阅读器正在读取空行?

c# - 两个具有相同命名空间的不同 DLL

visual-studio-2010 - 在2.0到3.5项目转换后,Visual Studio 2010编译器看不到Web引用 namespace

synchronization - Zookeeper 是否具有进程同步功能,还是仅用于控制进程?

c++ - 在 C++11 中等待多个条件变量的最佳方法是什么?

java - 限制生成的线程数量

c# - 具有优先级的信号量

multithreading - Lua中的抢占式多线程

c++ - 使用多个存储库的项目的 git 子模块的替代方案