Is there a trick to know how much time there's before the next timer call back event?

Hi everyone,

My application is supposed to launch some sound alarms in some particular situation. The sound alarm is supposed to last an given amount of time according to the situation: critical situation = 1min, mid critical = 30 s, non critical 5s.

Therefore I can not simply use Timer.Change method to modify the due time, I need also to know what amounts of time there’s still on the current timer.

Do you have any clue on how to achieve that in a simple manner? (I mean do not compare the datetime at the creation of the timer and the current datetime?)

I did not find any property of the timer to obtain such information but may be I have missed something?

would launching a thread and using thread.sleep work for you?

Thanks for your replies guys! So there’s no simple manner to achieve that, launching an other thread to decrement or sleep is normally trivial but the timers are launched using a date property events and I afraid to not succeed to perform thread safe mechanism.

What would be your choice in this situation ? Stopwatch? decrement? other?

Actually this is more complicated than a dice!

The specs are the following

The number of alarms is undefined. An alarm is composed of a pair (external alarm (buzzer on a relay) and a melodi alarm based on PWM).

For the moment, three alarms are defined
-Critical alarm that plays on internal and a pattern A and on external a pattern B
-Major alarm that plays on internal and a pattern C and on external a pattern D
-Notification alarm that play only on internal a pattern E.

Each pattern is composed of 8 tones.

Each alarm can be played during a while defined in a configuration file according to the source of the alarm (for example an over voltage will play critical alarm during 100 seconds while overhumidity will play during 50sec only) as a result, if an over voltage comes at t=0 and a overhumidity comes at t=40, the resulting alarm will ring during 100s.

However if OverHumidity comes a t=60s then the alarm will ring during 110s.

To complicate it more there’s a priority constrained.
If at t=0 a critical alarm fires for 100s and at 80s a notification alarms fires for a duration of 30s. Then at t=100s the critical alarms should stops and the notification should be fired for 10s

as you can imagine my code results in a spaghetti plate and do not work as expected

To resume I hate this part of the application design!

Can more than one alarm be sounded at a time? What if you setup a timer at the lowest common denominator (5s? 1s?) then have a variable that holds the time of the last Tick and also the time of the next alarm. Then in your Tick event handler you just need to compare the two and decide if you need to sound an alarm or just wait till the next tick.

Or maybe its even easier than that if there can only be one alarm at a time… Keep your timer stopped until you detect that you need an alarm. Then set its length and start it. Is there some complication I’m not quite getting?

My suggestion is to extend the timer class to handle the above situations.