C# 单线程程序的事件订阅限制

发布于 2024-12-09 12:22:41 字数 191 浏览 1 评论 0原文

我正在尝试监视在单线程程序中并行运行的许多 HPC 作业的状态,我正在订阅 OnJobState 引发的事件,并且当监视少至三个作业时,事件状态更改将丢失并且作业卡在运行状态。

我假设每个作业需要一个线程来捕获所有事件,但我找不到有关单线程程序中事件订阅限制的任何信息。

我本以为 .net 平台会将所有这些都排队,但事实似乎并非如此。

I'm attempting to monitor the status of many HPC jobs running in parallel in a single threaded program, I'm subscribing to events raised by OnJobState and when monitoring as few as three jobs event state changes will go missing and the job is stuck running.

I'm assuming I need a thread per job to catch all the events but I can't find any information about the limits of events subscripton in a single thread program.

I would have thought the .net platform would queue this all up but that doesn't appear to be the case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2024-12-16 12:22:41

默认情况下,事件是同步的。这意味着引发事件的对象只有在所有事件处理程序完成其工作后才会继续执行。事件处理程序将在与引发事件的对象相同的线程上运行。这导致了以下结论:

  1. .NET 框架无法对任何内容进行排队,因为事件是一个接一个地引发的
  2. 。您不应该在事件处理程序中进行繁重的计算。如果事件快速连续触发,则即使是适度的计算也应该避免。
  3. 如果您想要排队,则需要自己实现:在事件处理程序中,将有关新事件的信息添加到 一个线程安全队列并从另一个线程处理该队列。

Events are synchronous by default. That means that the object that raises an event will continue its execution only after all event handlers finished their work. The event handlers will run on the same thread as the object that raises the event. That leads to the following conclusions:

  1. The .NET framework can't queue anything, because the events are raised one after another
  2. You should not do heavy computing in event handlers. If the events are fired in rapid succession, even moderate computing should be avoided.
  3. If you want queuing, you need to implement it yourself: In your event handler, add the info about the new event to a thread safe queue and process this queue from another thread.
So要识趣 2024-12-16 12:22:41

我使这个问题更加笼统,以消除对 HPC 的困惑,看起来就像我无法控制事件处理程序的执行方式一样,所以我需要使其线程安全。

I made this question more general to remove the confusion over HPC, looks like I have no control over how my event hander is executed so I need to make it thread safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文