Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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!
Posted
Updated 1-Nov-11 22:48pm
v2

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.
 
Share this answer
 
Comments
paleGegg0 2-Nov-11 12:07pm    
Thanks for your guidance, helped a lot! :)
maybe you can disable event handling in the handler when an event comes in,then handle the event, and finally re-enable the handling?
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900