views:

525

answers:

6

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.

+2  A: 

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.

Justin Ethier
but i wanna be able to implement that in the code. imagine this service will be installed in many servers, i dont want to go to each server and create a scheduled task.
@user177883: You can create scheduled tasks programatically just like you can create services programatically.
Billy ONeal
+2  A: 

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.
Daniel A. White
+2  A: 
Thread.Sleep(1000*60*60);
erjiang
What happens if you try to stop the service while it's sleeping for an hour?
Phil
+7  A: 

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.

Franci Penov
+1  A: 
Thread.Sleep(TimeSpan.FromHours(1));

code more readable this way

Mehmet Taskopru
+2  A: 

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.

Al Bundy