Link to home
Start Free TrialLog in
Avatar of rameedev
rameedevFlag for India

asked on

Windows Service Timer

Hi All

i have a windows service that runs on a timer..On timer elapse it does a whole lot of things from pulling data from one database to putting it to another.I am using System.Timers.Timer class for the timer purpose.I am enabling the timer in the OnStart() function of the windows service.

My question is in the timer elapse event handler do i need to disbale the timer object and then at the end of the function..say if i have a try..catch and finally block..re-enable the timer object...By not doing this will the timer event handler function execute in a different next time it is invoked..Please guide me on this

Regards
Rameedev
Avatar of Yurich
Yurich
Flag of New Zealand image

The timer will "tick" on every period of time you specified as an interval and these intervals will be the same.

So, the scheme is: as soon as it's triggered, you can do whatever you want and for how long you want to do but timer is already counting from the moment it was triggered regardless on the ammount of work you need to do in your timer event handler.
Avatar of rameedev

ASKER

Hi
Thanks for the quick response

Lets take an example.If my timer interval is say run every one minute.Now in the timer elapse event handler whatever work i am doing if it takes more than a minute then it's a problem isnt it. so what basically i was thinking is disable at the start of the function and enable the timer at the end..Preferably in  Finally block in case of exceptions...What do you think of this

Regards
Rameedev
ASKER CERTIFIED SOLUTION
Avatar of Yurich
Yurich
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahhh... i dont call a separate function every thing happens in the same timer elapse handler

private void HandleTimerElapse(object sender,ElapsedEventArgs e)
  {
          try
          {
                 //Call stored procedurep1
                 //Call stored procedurep2
                 //Call stored procedurep3
          }
          catch(Exception ex)
          {
             LogError in Error File
          }
          finally
          {

          }
  }

so from what you are telling i can enable and disable timer object in the same function...I will do check on a separate Function call inside the timer elapse handler..if it works the way you told

Regards
RameeDev
since finally is called even if the exception is thrown, i would put timer.Start() in finally only if I wanted to start a new cicle even if the current one is failed to finish... otherwise, i would put it after the finally block - in which case it would be guaranteed that it's called only in the case of success.

And yes, you can enable and disable timer in the same function. If all operations take place inside your handler, there shouldn't not be any problems starting your timer after it's all done.

regards