Scheduled task windows service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello...
I need to add a timer to a windows sevice to start a program every day at
the same time. Because timers aren't set up to do this (run at a specific
time), I know I need to check the time and establish an interval so the timer
knows when to start my program. Then I guess I need to check the time and
re-establish the interval again. I'm sure this is a common thing to do, and
I am trying to find some examples to look at. I'm just looking for a quick
and dirty example.
Thanks for any help...
 
You're on the right track.

In your service initialization code:
- Create a System.Timers.Timer object
- Set the Interval (perhaps to 1000, to check the current time once every
second, or 10,000 to check every minute)
- Attach to the timer's Elapsed event
- Enable the timer

When the Elapsed event fires:
- check the system time
- if the system time is the time of day you are waiting for, run the other
app; otherwise do nothing

You will probably want to keep a flag that indicates the last day the
program was run, and when you are checking the current system time, see if
it is not only on, but also possibly past, the time you want to run your
other program, and also check the last-run flag to make sure it is not
today. This way you won't miss a day if the system gets busy and the timer
doesn't have the opportunity to fire on the exact second or minute you're
watching for.
 
Back
Top