哪种更适合用于定期工作:服务或计划任务? [英] Which is better to use for a recurring job: Service or Scheduled Task?

查看:214
本文介绍了哪种更适合用于定期工作:服务或计划任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务需要每30秒运行一次。我可以做两件事之一:

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


  1. 编写一个运行任务的命令行应用程序,等待30秒,运行它再次然后退出。

  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

编写一个服务,在每次运行之间等待30秒时重复运行一个任务。

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

数字1更不重要,在我看来,我会选择默认做这种方式。我想知道吗?有什么原因我应该使这个服务,而不是一个计划的任务?

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?

推荐答案

我最近读了一个很好的博客文章这个问题。为什么您应该编写服务以运行重复性作业,这是很好的理由。此外,此问题之前已提出过:

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:

http://stackoverflow.com/questions/390307/windows-service-vs-scheduled-task
Windows服务或计划任务,我们更喜欢哪一个?

使用计划任务的一个优点是,如果运行服务涉及一些潜在风险,例如内存泄漏或挂起网络连接,则Windows服务可能会挂起时间长,对其他用户有不利影响。另一方面,计划的任务被写为短时间运行,因此即使它泄漏,效果也被最小化。

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.

另一方面,某人在一个的上述问题评论说,调度程序具有在1分钟范围内某处的精度限制,因此您可能会看到调度程序无法每30秒精确地运行您的任务。

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.

这篇关于哪种更适合用于定期工作:服务或计划任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆