views:

199

answers:

2

Hi,

In my .net windows service, when my timer elapses and my main method is called, is it best practice to put the timer in sleep mode?

This is in case my main method runs for too long, and the timer elapses before the previous calls main method finishes its execution.

+1  A: 

It's not really a "main method" but I suppose you just mean the method which gets called as a result of the elapsed event. It depends. Do you need it to run every interval or do you just need the process to start at each interval and if it is already running, don't do anything.

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

BobbyShaftoe
+1  A: 

On top of what BobbyShaftoe mentioned, I would like to suggest to use a try/catch/finally block and put the code to restart the timer in the finally block. So in case when an exception hits, your timer will still restart and run later. Unless you would prefer it to stop indefinitely.

faulty