Should I put my timer to sleep when my main method is running on a Windows service?

On my windows.net service, when my timer expires and my main method is called, is it better to use the sleep timer?

This is in case my main method takes too long and the timer expires before the main method of previous calls has finished executing it.

-1


source to share


2 answers


This is not really a "main method", but I suppose you just mean the method that is called as a result of a past event. It depends. You need it to run every interval, or you just need it to start at every interval, and if it is already running, do nothing.



If the latter is what I suspect, since you are asking about it, then yes, go ahead and stop your timer as the first statement in your past event handler. Then start the timer again when you're done with everything else in your "main method".

+1


source


In addition to what BobbyShaftoe mentioned, I would like to suggest using a try / catch / finally block and put the code to restart the timer in a finally block. This way, in case of an exception, your timer will restart and start later. Unless you prefer it to stay indefinitely.



+1


source







All Articles