tags:

views:

36

answers:

1

I'd like to do some work every 5 seconds. When I say every 5 seconds I mean on 00:00, 00:05, 00:10, 00:15, 00:20 according to the system clock. Not every 5 seconds since the timer is started.

Is there a .net timer which can do this already - I can't find one?

I can think of two work arounds...

  1. Have a very high resolution timer and read the system clock each timer event. Fire off my work immediatelly after each whole 5 secods has passed.

  2. Have a very high resolution timer and read the system clock each timer event. Fire off a second timer which has a resolution of 5 seconds immediatelly after each whole 5 secods has passed.

Not happy with either approach - the first puts unnecessary load on the cpu (depending on how high the reolution is). The second method might drift after a very long time. I don't know whether that's true or not.

+1  A: 

Not directly, but you could set a one short timer to trigger at the next 5s multiple, and in that event handler set up the every 5s schedule.

Richard