[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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
0
rameedev
Asked:
rameedev
  • 3
  • 2
1 Solution
 
YurichCommented:
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.
0
 
rameedevAuthor Commented:
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
0
 
YurichCommented:
well, that's a lot of operations to run for the whole minute ;) but if you really want to intiate new "ticking" at the end of everything what's happening in your event handler, like:

1 minute of ticking
some time to perform operations
1 minute of ticking
...

that can be a bit tricky ... if you call a function which is supposed to perform something and that function will call another function, the following way WILL NOT WORK:

private void timer_Tick( object sender, System.EventArgs e)
{
  timer.Stop(); // it will
  function2();   // call this function but
  timer.Start(); // jumps reight here...
}

private void function2()
{
  function3();
}

but this one WILL WORK

private void timer_Tick( object sender, System.EventArgs e)
{
  timer.Stop()
  function2();
}

void function2()
{
  function3();
}

void function3()
{
  //...
  timer.Start();
}

hope it helps ;)
0
 
rameedevAuthor Commented:
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
0
 
YurichCommented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now