在.NET Framework 2.0上,AutoResetEvent和ManualResetEvent從EventWaitHandle繼承。 EventWaitHandle類有4個不同的構造函數。 3個構造函數支持爲該事件命名。另一方面,ManualResetEvent和AutoResetEvent都不支持命名,並提供了接收initialState的單個構造函數。我可以簡單地繼承EventWaitHandle並編寫自己的支持所有構造函數重載的類的實現,但如果不需要,我不喜歡重新創建輪子。我的問題是:爲什麼AutoResetEvent和ManualResetEvent不支持構造函數中的名稱?
- 命名事件是否存在特殊問題?
- 你知道爲什麼微軟不支持它嗎?
- 您是否有比從以下示例中的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) { } }
您的解決方案與我的解決方案類似。我從派生類構造函數中調用EventWaitHandle,並直接使用它。 – Ikaso 2010-05-12 09:57:35
@lkaso我認爲這比創建一個派生類更直截了當......不幸的是,我沒有太多關於設計選擇的理由。 – Kiril 2010-05-12 15:50:48