控制事件发射频率虽然安装/拆卸事件。不好的做法? [英] Control event firing frequency though attaching/detaching to the event. Bad practice?

查看:71
本文介绍了控制事件发射频率虽然安装/拆卸事件。不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我提供了这个问题的答案如何你控制事件在C#中烧制它,总之,询问以下:




这是一个比我想事件每当从相机接收到一个新的帧的火灾。然而,这种情况更加频繁...我如何控制事件在?


< /块引用>

在我的答案我提供了下面的代码,今天早上我发现我有2 downvotes没有任何评论。我关注的不是主要代表的损失,而这是我自己使用的各种应用逻辑,那downvotes可能表明我的实现是不好的做法还是坏的表现。于是,我问这个问题的澄清,如果有什么不对的安装/以这种方式分离事件控制器?



 公开为MyObject ()
{
MyTimer =新System.Timers.Timer(100); // 10赫兹
MyTimer.Elapsed + =新ElapsedEventHandler(OnTimedEvent);
MyTimer.Enabled = TRUE;
}

私人无效ImageDataUpdated(对象发件人,EventArgs五)
{
//从事件中分离,以保持它从发射直到计时器事件已经被解雇。
MyImageObject.Update - =新UpdateEventHandler(ImageDataUpdated);

//做的东西
}

私有静态无效OnTimedEvent(对象源,ElapsedEventArgs E)
{
//(重)连接到事件处理程序。
MyImageObject.Update + =新UpdateEventHandler(ImageDataUpdated);
}



此外,我写了下面的,指的是它可以检查是否你居然准备数据发送到用户之前有任何用户。在某些情况下,这可能导致降低CPU的使用率。对我来说,似乎是正确的(我再次这样做我自己),但有什么不对这个说法?




使用这种策略还有就是你的,阻止这样做的额外工作的基本图像对象,而事件处理程序分离一个很好的机会(中当然,这取决于图像对象的实现)。有机会,你要保存的CPU周期为你自己的图像处理。



解决方案

我想他们认为动态地添加和删除事件的基础上,一个不相关的计时器,是一件坏事。虽然这是一个巧妙的方法,可能是有用的地方哪天的,我倾向于同意他们的意见。对其他对象的事件处理程序的外部控制并不表明良好的封装。



另一种方法是简单地检查有足够的时间已经过去的在接收事件处理程序电话之间,并决定是否要处理与否。这是逻辑如何工作的实时游戏或类似的应用程序和手段,你不经常连接,并从外面断开事件处理程序更加一致。



的开销实际事件自称是相当小的,所以不用担心每帧打了几个电话,只要慢的部分(即该位做实际工作)没有执行每一次。


Yesterday I provided an answer to the question How do you control event firing in C#? which, in short, asks the following:

"There is an event that fires whenever a new frame is received from the camera. However, this happens more frequently than I'd like ... How can I control when the event fires?"

In my answer I provided the code below, and this morning I found that I had 2 downvotes without any comments. My concern is not mainly the loss of rep, but rather that this is logic that I am using myself in various applications, and that the downvotes could indicate that my implementations are bad practice or bad for performance. Thus, I am asking this question to clarify if there is anything wrong with attaching/detaching event controllers in this manner?

public MyObject()
{    
   MyTimer = new System.Timers.Timer(100); // 10 Hz
   MyTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
   MyTimer.Enabled = true;
}

private void ImageDataUpdated(object sender, EventArgs e) 
{
   // detach from the event to keep it from firing until the timer event has fired.
   MyImageObject.Update -= new UpdateEventHandler(ImageDataUpdated);

    // do stuff
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    // (re-)attach to the event handler.
   MyImageObject.Update += new UpdateEventHandler(ImageDataUpdated); 
}

In addition I wrote the following, referring to that it is possible to check if you have any subscribers before actually preparing the data to send to the subscriber. And in some cases this can lead to reduced CPU usage. To me it seems right (again I do this myself), but is there anything wrong with this statement?

Using this strategy there is a good chance that you are preventing the underlying image object from doing additional work while the event handler is detached (of course this depends on the implementation of the image object). Chances are that you are saving CPU-cycles for your own image processing.

解决方案

I imagine they felt that dynamically adding and removing events, based on an unrelated timer, was a bad thing. While that is a neat trick that might be useful somewhere, someday, I tend to agree with them. External control over other object's event handlers does not indicate good encapsulation.

The alternative is to simply check that sufficient time has elapsed in the receiving event handler between calls and decide whether to process or not. This is more consistent with how logic works in real-time games or similar apps and means you are not constantly connecting and disconnecting event handlers from the outside.

The overhead of an actual event call itself is quite small, so do not worry about a few calls per frame, so long as the "slow part" (i.e. the bit doing the actual work) is not executing every time.

这篇关于控制事件发射频率虽然安装/拆卸事件。不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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