What is better to use for repetitive work: a service or a scheduled task?

I have a task that should start every 30 seconds. I can do one of two things:

  • Write a command line application that launches a task once, waits 30 seconds, starts it again and then exits. I can schedule this task with scheduled tasks on Windows to run every minute

  • Write a service that runs the task multiple times, waiting 30 seconds between each run.

The number 1 is more trivial, in my opinion, and I would decide to do it by default. I guess Is there a reason why I should make this a Service, not a planned one? What are the pros and cons of both and which would you choose at the end?

+4
source share
4 answers

I recently read a good blog post about this issue . This has many good reasons why you shouldn't write a service to run repetitive work. In addition, this question has been asked before:

fooobar.com/questions/280848 / ... Windows service or a scheduled task that we prefer?

One of the benefits of using the scheduled task is that if there is some potential risk associated with starting the service, such as a memory leak or a hanging network connection, then the Windows service can potentially hang for a long time, which negatively affects other users. On the other hand, the scheduled task is recorded as short, therefore, even if it proceeds, the effect is minimized.

On the other hand, one of the above questions noted that the scheduler has an accuracy limit somewhere around 1 minute, so you can see that the scheduler cannot complete your task every 30 seconds with accuracy.

Obviously, there are a few trade-offs to consider, but hopefully this helps you make the right decision.

+8
source

If you try to run every 30 seconds, I would choose option 2. This is pretty much constant work, in this case. The overhead of starting and stopping the process is probably higher than the process itself, especially if you use the appropriate timer.

If you perform a task that is performed once a day (or several times a day), I would choose option 1 - using the scheduled task.

+3
source

The task scheduler in the windows seems a bit fluffy in my opinion. I think you will get a more reliable result executed as a service.

In addition, a service can store resources in memory, such as reading input from a file, and you only need to do this when the service starts, and not every 30 seconds.

+2
source

30 seconds is a fairly short interval (relatively speaking) between processing cycles. Like others, I have my own problems with the task scheduler, and I am afraid that such a short interval will only exacerbate the problems that may arise if you take this approach. If it were my project, I would almost certainly go to the service.

0
source

All Articles