views:

128

answers:

3

I have a c# windows service that needs to execute a database query every 60 seconds (or whatever interval is set in the config file). I'm using Thread.sleep(60) in a while loop to accomplish this. Is there a better way to do this?

Thanks

+6  A: 

You can use a System.Threading.Timer to run your code every 60 seconds instead of doing it in a sleeping thread.

Reed Copsey
+4  A: 

Use a Timer to fire the process every 60 seconds rather than force the process to sleep.

Justin Niessner
+4  A: 

It depends on what you want to do. If you want a (more) exact method you may want to use System.Threading.Timer. One thing about thread.sleep is that it really is "sleepatleastuntil...". The processor will ignore your thread until the sleep time is past. The next time it would give your thread priority, after the sleep time is past, it will.

This will normally be very close to the desired interval, but could be longer. Don't yield unless you really intend to yield.

Russell Steen