I’m building an application that needs to fire an event every second, but it needs to be at the top of every second. Firing between seconds is not good enough. I don’t think a timer will be acceptable since, while I can set it to fire every second, I can not tell it to start at the top of the second. And it will probably drift slightly over time.
Is there maybe a system level event that i can hook into that will fire as the system time updates?
Any other ideas on how I could accomplish this?
I did this before with great success on a moble device. This method also eliminates drift (jitter).
In my specific case, my timer interval was 100ms, so I could catch second changes within 100ms. You will set your interval to 10ms.
Depending on your needs,youmaywill want to consider using a server-based timer.[update] It is faster to store the time as Integer (tick count) and not a DateTime. On initialization, you loop until the second changes, then store the tick count (Environment.TickCounts). When current tick count minus saved tick count is greater than 1000, you know a second has passed.