在发出信号后Dispose()一个EventWaitHandle是否安全?

时间:2022-01-22 03:33:03

I have a situation where I'm creating a number of IDisposable objects that encapsulate an EventWaitHandle instance each, so that various interested parts of my app can wait on them. This instance is not directly accessible to any code outside of its owner object. It can only be accessed indirectly through wrapper calls.

我有一种情况,我正在创建一些IDisposable对象,每个对象封装一个EventWaitHandle实例,以便我的应用程序的各种感兴趣的部分可以等待它们。此实例不能直接访问其所有者对象之外的任何代码。它只能通过包装调用间接访问。

Once an object signals it's done, it is no longer useful, so it is disposed by a central manager object and tossed from its list of references.

一旦一个对象发出信号就完成了它,它就不再有用了,所以它由一个*管理器对象处理,并从它的引用列表中抛出。

The question now is, what to do with the encapsulated EventWaitHandle? Naturally, it should be disposed as well, and sooner rather than later, to prevent my app from leaking OS handles.

现在的问题是,如何处理封装的EventWaitHandle?当然,它应该被处理,并且应该尽快,以防止我的应用程序泄漏操作系统句柄。

But is it safe to do that synchronously, immediately after the event is signaled by its owner object? What can happen if there are threads still waiting to be released (i.e. blocking inside a call to WaitOne())?

但是,在事件由其所有者对象发出信号后立即同步执行此操作是否安全?如果有线程仍在等待释放(即在WaitOne()调用内阻塞)会发生什么?

What is the recommended approach here?

这里推荐的方法是什么?

1 个解决方案

#1


1  

I guess it's time to close this one with my own findings.

我想是时候用自己的发现来结束这个了。

I couldn't find any direct guidelines in the docs, but decided to follow the information I extracted from Raymond Chen's blog. In his posts (I forget the exact links) he mentions Win32 API rules dictate that event's handle must remain valid for the duration of waiting. In unmanaged world this means at least one handle to the event must remain open.

我在文档中找不到任何直接指导,但决定遵循我从Raymond Chen的博客中提取的信息。在他的帖子中(我忘记了确切的链接),他提到Win32 API规则规定事件的句柄必须在等待期间保持有效。在非托管世界中,这意味着事件的至少一个句柄必须保持打开状态。

AFAIU, .NET's implementation uses Win32 API under the hood, with each EventWaitHandle instance corresponding to a separate unmanaged event. When EventWaitHandle.Dispose() closes the only handle to the underlying unmanaged event, this effectively renders the event instance invalid.

AFAIU,.NET的实现使用Win32 API,每个EventWaitHandle实例对应一个单独的非托管事件。当EventWaitHandle.Dispose()关闭底层非托管事件的唯一句柄时,这有效地使事件实例无效。

In short, the right approach seems to be to build a parallel infrastructure through which event publisher can notify potential listeners that event will be going away soon. The publisher must then wait until all the listeners have "unsubscribed" (i.e. stopped waiting) before proceeding to Dispose() the event instance.

简而言之,正确的方法似乎是建立一个并行基础设施,通过该基础设施,事件发布者可以通知潜在的听众,事件将很快消失。然后,发布者必须等到所有侦听器都“取消订阅”(即停止等待),然后再继续Dispose()事件实例。

It's a lot of book-keeping, but ultimately, it only seems right. Hope this helps clear things for others, too.

这是一个很多的簿记,但最终,它似乎是正确的。希望这也有助于为他人清除事情。

#1


1  

I guess it's time to close this one with my own findings.

我想是时候用自己的发现来结束这个了。

I couldn't find any direct guidelines in the docs, but decided to follow the information I extracted from Raymond Chen's blog. In his posts (I forget the exact links) he mentions Win32 API rules dictate that event's handle must remain valid for the duration of waiting. In unmanaged world this means at least one handle to the event must remain open.

我在文档中找不到任何直接指导,但决定遵循我从Raymond Chen的博客中提取的信息。在他的帖子中(我忘记了确切的链接),他提到Win32 API规则规定事件的句柄必须在等待期间保持有效。在非托管世界中,这意味着事件的至少一个句柄必须保持打开状态。

AFAIU, .NET's implementation uses Win32 API under the hood, with each EventWaitHandle instance corresponding to a separate unmanaged event. When EventWaitHandle.Dispose() closes the only handle to the underlying unmanaged event, this effectively renders the event instance invalid.

AFAIU,.NET的实现使用Win32 API,每个EventWaitHandle实例对应一个单独的非托管事件。当EventWaitHandle.Dispose()关闭底层非托管事件的唯一句柄时,这有效地使事件实例无效。

In short, the right approach seems to be to build a parallel infrastructure through which event publisher can notify potential listeners that event will be going away soon. The publisher must then wait until all the listeners have "unsubscribed" (i.e. stopped waiting) before proceeding to Dispose() the event instance.

简而言之,正确的方法似乎是建立一个并行基础设施,通过该基础设施,事件发布者可以通知潜在的听众,事件将很快消失。然后,发布者必须等到所有侦听器都“取消订阅”(即停止等待),然后再继续Dispose()事件实例。

It's a lot of book-keeping, but ultimately, it only seems right. Hope this helps clear things for others, too.

这是一个很多的簿记,但最终,它似乎是正确的。希望这也有助于为他人清除事情。