I am creating a WindowsService which has a Timer bound to an EventHandler which has an infinite loop; I want to be able to fire this Timer once in the service’s OnStart() method, then stop the Timer in the service’s OnStop(). How can I force the Timer to fire only once?
Also, is this the best design possible for controlling infinite loops within a WindowsService? Would handling it inside another thread (perhaps background thread) be better?
Yes. This sounds like you’d be better off using a single background thread instead of a timer. The thread could enter your loop state until you signal that it’s time to exit (in the
OnStopmethod).The new Cancellation support in .NET 4 provides a fairly clean way to “cancel” your infinite loop…