“波纹"指的是状态更改事件中的处理 [英] "Ripple" handling in state changed event

查看:101
本文介绍了“波纹"指的是状态更改事件中的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我的C#应用​​程序侦听来自嵌入式系统的状态更改事件(状态为true或false).根据状态,将执行某些代码.

我的问题是,当状态更改时,事件中将出现涟漪".例如,如果状态从false更改为true,则我将连续获得x个true/false事件,直到状态最终稳定为true(相同的情况为true-> false).这意味着我的代码将执行错误状态和真实状态的代码加载,这是完全不可接受的.

在事件处理中,我只是

Hello all!

My C# application listen state changed event (state is true or false) from embedded system. According state certain code will be executed.

My problem is that when state changes there will be "ripple" in events. For example if state changes from false to true I will get x pcs true/false events in row until state settles finally to true (same happens true -> false). That means my code will execute loads of code for false state and true state, that is totally unacceptable.

in event handling I have simply

if(true)
//execute true methods
else
//execute false methods



希望您对这个问题有一些想法和指导.

希望你有主意:)

干杯!



I hope you got some ideas and guidance for this problem.

Hope you got idea :)

Cheers!

推荐答案

尝试保留说出10个先前状态的列表并取其平均值.

您还可以保留前10个时间值并获取最后1秒的稳定值.
Try keeping a list of say 10 previous states and averaging them.

You can also keep a time value for the previous 10 and and take the last 1 second stable value.


也许您可以在事件进入时在处理程序中禁用事件处理,然后处理事件,最后重新启用该处理?
maybe you can disable event handling in the handler when an event comes in,then handle the event, and finally re-enable the handling?


如果所有状态更改事件都相同,即您收到了
StateChange:true
StateChange:true
StateChange:true
StateChange:true
StateChange:true
StateChange:true

...然后在您的处理程序中简单地检查状态是否与上次报告的状态相同,如果是,则不执行任何操作.

如果不是这样,则需要使用计时器来延迟更新,直到系统稳定为止,该计时器在每次获取另一个更新时都会重置.这有点丑陋,但是如果没有办法知道最后一次真正的更新,那就没有其他办法可以避免多次刷新.
If all the state change events are the same, i.e. you receive
StateChange: true
StateChange: true
StateChange: true
StateChange: true
StateChange: true
StateChange: true

... then in your handler simply check if the state is the same as the last reported one, and if so, do nothing.

If not, you need to delay update until the system has stabilised, using a timer which you reset each time you get another update. That''s a bit ugly but if there''s no way to know which is the last real update there''s no other way to avoid multiple refreshes.


这篇关于“波纹"指的是状态更改事件中的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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