views:

104

answers:

1

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?

+2  A: 

I did this before with great success on a moble device. This method also eliminates drift (jitter).

  1. Set your timer to a small interval.
  2. Save time.
  3. On tick event,
  4. Compare current time to saved time.
  5. If time different (seconds changed),
    1. then do something
    2. and save time

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, you may will 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.

AMissico
Server based timer: http://msdn.microsoft.com/en-us/library/tb9yt5e6.aspx
chilltemp