Tutorial :Windows service that will run every hour


I m able to build a windows service and install it.

I m curious how can i run this service every hour ? I want it to run every hour periodically.

I also need to know the hour range that it s running so that I can store it somewhere.

How can i do that?

Edit : This service will be installed on many machines, therefore, I dont want to create a scheduled task say on 100 servers.


If you want a task to run on a regular interval as opposed to constantly, you should look into using the Task Scheduler.

If you need your code to be a service, but to be "activated" every hour, the easiest approach would be to make your service a COM object and have a simple task scheduled every hour that invokes a jscript/vbscript that creates your COM object and calls simple method on it.

The alternative is to use any of the wait APIs to "waste" an hour without consuming cycles.

Note that you also have to consider some interesting design decisions that depend on what your scenario is:

  • how is your service going to be started if it crashes or is stopped by the user?
  • if you are started after more than an hour, should you run again or do you need to wait to get on the exact hourly schedule?
  • how do you keep track of the last "activation" time if the timezone or the day-light saving time has changed while you were not active?
  • does your service prevent the computer from going to sleep/hibernate on idling or when the laptop cover is closed? if not, do you need to awake the computer on the hour to get your service working on your schedule?

Some of those are taken care of by the task scheduler, so I would strongly recommend going that route vs. waiting for an hour in your code.


You could create a scheduled task that runs every hour, to either run the service or send a message to "wake it up". Then, either pass in the current time in the scheduled task request, or just have your program pick up the current time when it wakes up.

Task Scheduler Managed Wrapper can help you set this up programmatically; you can google for other resources as well.


There are a couple options.

  1. You could sleep for an hour.
  2. You might be better suited for a Scheduled Task, not a service.





code more readable this way


Thread.Sleep() solution will make sure that your service will run in one hour intervals, not every hour i.e. each task will be started at 1 hour + time to run the task. Consider using a Timer within your service. This will be a more robust solution since you have a control when to run a task, monitor its progress etc. Just remember that each Timer event will be fired in a different thread and if the task takes longer than one hour to run you might have to wait for the first task to finish to avoid concurrent tasks.


Task schedulers may be a good idea but services are designed to do this. Services gets installed easily and logs things properly. All you need to do is, at start of service, you can install a system timer (System.Threading.Timer) or there is also one more timer.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »