Execute timer in sync with system clock #2927
-
Hi, I'm currently trying to use Sming for building a clock. I'm using the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Ok, I was able to solve this myself. I basically build a "rookie" crontab by having a timer run every second and comparing the current date time inside its callback (1s is precise enough for my use case). E.g.: auto dt = DateTime(SystemClock.now());
// Only continue if it's the 5th minute of the current hour
if (dt.Minute % 5 != 0 && dt.Second < 1)
return; If there is some built in utility or library I overlooked, feel free to let me know though :) |
Beta Was this translation helpful? Give feedback.
-
It's risky checking the clock since we cannot guarantee the time is monotonic: NTP updates may cause forward or backward jumps. It's also not efficient using DateTime like this. I'd recommend just using a
We probably shouldn't schedule the first event until the clock's been set, so let's do that in the clock change handler:
|
Beta Was this translation helpful? Give feedback.
It's risky checking the clock since we cannot guarantee the time is monotonic: NTP updates may cause forward or backward jumps. It's also not efficient using DateTime like this.
I'd recommend just using a
Timer
configured to fire after the desired interval. Something like this: