Windows service that will run every hour

I can create a Windows service and install it.

I am curious how can I launch this service every hour? I want it to run every hour periodically.

I also need to know the range of hours that it runs so that I can store it somewhere.

How can i do this?

Edit: This service will be installed on many machines, so I do not want to create a scheduled task on 100 servers.

+6
c # cron windows-services
source share
7 answers

If you want the task to run at regular intervals rather than constantly, you should study the Task Scheduler .

If you need your code for maintenance, but for β€œactivating” every hour, the easiest approach is to make your service a COM object and do a simple task every hour that calls jscript / vbscript, which creates your COM object and calls a simple method on it .

An alternative is to use any of the pending APIs to β€œwaste” hours without using cycles.

Please note that you also need to consider some interesting design decisions that depend on your scenario:

  • How will your service start if it works or stops by the user?
  • if you start after more than an hour, do you have to start again or do you need to wait to get the exact hourly schedule?
  • How do you track the latest activation time if the time zone or daylight saving time changed when you were not active?
  • Does your service not allow your computer to go to sleep / idle mode or when the laptop cover is closed? if not, do you need to wake up the computer in an hour to make your service work with your schedule?

Some of them take care of the task scheduler, so I highly recommend taking this route against waiting an hour in your code.

+12
source share

You could create a scheduled task that runs every hour to either start the service or send a message to β€œwake up”. Then either go to the current time in the request for the scheduled task, or simply run the program when it wakes up.

Task Scheduler Managed Wrapper can help you install this programmatically; You can also use Google for other resources.

+3
source share

There are several options.

  • You can sleep for an hour.
  • Perhaps you are better suited for a scheduled task rather than a service.
+3
source share
Thread.Sleep(1000*60*60); 
+2
source share
 Thread.Sleep(TimeSpan.FromHours(1)); 

the code is more readable this way

+2
source share

Thread.Sleep() solution guarantees that your service will work after one hour, and not every hour, that is, each task will be launched after 1 hour + time to complete the task. Consider using Timer in your service. This will be a more reliable solution, since you have control when completing a task, monitoring its progress, etc. Remember that each Timer event will be fired in a different thread, and if the task takes more than an hour to start you may have to wait until the first task is completed to avoid parallel tasks.

+2
source share

Task schedulers may be a good idea, but services are designed for this. Services are easily installed and recorded correctly. All you have to do is set the system timer (System.Threading.Timer) when starting the service, or there is another timer.

0
source share

All Articles