在单独的线程上提升事件 [英] Raising events on separate thread

查看:141
本文介绍了在单独的线程上提升事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要处理实时数据的组件,并以相当快速的方式将数据广播给听众(具有大约100纳秒级别的准确性,甚至比我能做到的要好)目前我正在提出一个来自我的代码的用户可以订阅的事件。但是因为在C#事件处理程序运行在引发事件的同一个线程上,所以引发事件的线程将被阻止,直到所有用户完成处理事件。我不能控制订阅者的代码,所以他们可以在事件处理程序中执行任何耗时的操作,这可能会阻止播放的线程。

I am developing a component which needs to process the live feed and broadcast the data to the listeners in pretty fast manner ( with about 100 nano second level accuracy, even less than that if I can do that) Currently I am raising an event from my code which subscriber can subscribe to. However because in C# event handlers run on the same thread which raises the event, my thread which raises the event will be blocked until all subscribers finish processing the event. I do not have control on subscribers' code, so they can possibly do any time consuming operations in event handler, which may block the thread which is broadcasting.

这样做,我可以将数据广播给其他用户,但仍然可以播放相当快的东西?

What can I do so that I can broadcast the data to other subscribers but can still broadcast the stuff quite fast??

推荐答案

100 ns是一个非常强硬的目标。我相信它会深入了解你正在做什么以及为什么要打这种表现。

100 ns is a very tough target to hit. I believe it will take a deep understanding of what you're doing and why to hit that kind of performance.

然而,异步调用事件订阅者很容易解决。
已经回覆了此处,还有谁还有Jon Skeet。

However, asynchronously invoking event subscribers is pretty easy to solve. It's already answered here by, who else, Jon Skeet.

foreach (MyDelegate action in multicast.GetInvocationList())
{
    action.BeginInvoke(...);
}

编辑:
我也应该请注意,您需要运行实时操作系统以提供紧密的性能

这篇关于在单独的线程上提升事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆