通过附加/分离事件来控制事件触发频率。糟糕的做法? [英] Control event firing frequency though attaching/detaching to the event. Bad practice?

查看:145
本文介绍了通过附加/分离事件来控制事件触发频率。糟糕的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我提供了一个问题的答案如何您是否控制C#中的事件触发?,简而言之,请求以下内容:

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


有一个每当从相机接收到新的帧时触发事件,但是这种情况比我想要的更频繁...如何控制事件触发的时间?

在我的答案中,我提供了下面的代码,今天早上我发现我有2个downvote没有任何意见。我的关注不在于代码丢失,而是这是我在各种应用程序中使用自己的逻辑,而且这些下拉列表可能表明我的实现是不好的做法或对性能不利。因此,我提出这个问题来澄清如果以这种方式附加/分离事件控制器有什么问题吗?

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); 
}

另外我写了以下内容,指的是可以检查在实际准备数据发送给用户之前,您有任何订阅者。在某些情况下,这可能会导致CPU使用率下降。对我来说似乎是正确的(我自己也是这样做的),但是这个说法有什么问题吗?

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?


使用此策略可以防止底层图像对象在事件处理程序分离时执行其他工作当然这取决于图像对象的实现)。有可能您正在为自己的图像处理节省CPU周期。

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天全站免登陆