Which is better to use for a recurring job: Service or Scheduled Task?

回眸只為那壹抹淺笑 提交于 2019-12-06 05:05:57

问题


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?


回答1:


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.




回答2:


If you're trying to run every 30 seconds, I'd go for option 2. This is pretty much a continually running job, in that case. The overhead of starting and stopping the process is probably higher than the process itself, especially if you use an appropriate timer.

If you make a job that is running once a day (or a few times a day), then I'd go for option 1 - using a scheduled task.




回答3:


The task scheduler in windows seems a bit flakey in my opinion. I think you would get a more reliable result running as a service.

Also, a service could keep resources in memory, such as reading input from a file, and only have to do this at start-up of the service, not every 30 seconds.




回答4:


30 seconds is a pretty short interval (relatively speaking) between processing cycles. Like the others I have my concerns about the task scheduler and I am afraid such a short interval will only compound the issues you might encounter if you took that approach. If this were my project I would almost certainly go with the service.



来源:https://stackoverflow.com/questions/1430177/which-is-better-to-use-for-a-recurring-job-service-or-scheduled-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!