C# – Which is better to use for a recurring job: Service or Scheduled Task

c++cmdscheduled-taskswindows-server-2008

I have a task that needs to run every 30 seconds. I can do one of two things:

  1. Write a command line app that runs the task once, waits 30 seconds, runs it again and then exits. I can schedule this task with Scheduled Tasks in Windows to run every minute

  2. Write a Service that runs a task repeatedly while waiting 30 seconds in between each run.

Number 1 is more trivial, in my opinion and I would opt to do it this way by default. Am I wimping out? Is there a reason why I should make this a Service and not a scheduled task? What are the pros and cons of both and which would you pick in the end?

Best Solution

I read a nice blog post about this question recently. It goes into a lot of good reasons why you should not write a service to run a recurring job. Additionally, this question has been asked before:

https://stackoverflow.com/questions/390307/windows-service-vs-scheduled-task Windows Service or Scheduled Task, which one do we prefer?

One advantage of using the scheduled task, is that if there is some potential risk involved with running the service such as a memory leak or hanging network connection, then the windows service can potentially hang aroung for a long time, adversely affecting other users. On the other hand, the scheduled task is written to be short running, so even if it does leak, the effect is minimised.

On the other hand, someone in one of the above questions commented that the scheduler has a limit of accuracy of somewhere in the range of 1 minute, so you may see that the scheduler is unable to run your task every 30 seconds with accuracy.

Obviously there are a number of tradeoffs to consider, but hopefully this will help you make a good decision.

Related Question