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

寵の児 提交于 2019-12-04 09:13:31
1800 INFORMATION

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.

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.

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.

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.

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