简体   繁体   English

预定任务或Windows服务

[英]scheduled task or windows service

I have to create an app that will read in some info from a db, process the data, write changes back to the db, and then send an email with these changes to some users or groups. 我必须创建一个应用程序,它将从数据库中读取一些信息,处理数据,将更改写回数据库,然后将包含这些更改的电子邮件发送给某些用户或组。 I will be writing this in c#, and this process must be run once a week at a particular time. 我将在c#中编写此代码,此过程必须在特定时间每周运行一次。 This will be running on a Windows 2008 Server. 这将在Windows 2008 Server上运行。

In the past, I would always go the route of creating a windows service with a timer and setting the time/day for it to be run in the app.config file so that it can be changed and only have to be restarted to catch the update. 在过去,我总是会使用计时器创建一个Windows服务的路径,并设置它在app.config文件中运行的时间/日期,以便可以更改它,只需重新启动即可捕获更新。

Recently, though, I have seen blog posts and such that recommend writing a console application and then using a scheduled task to execute it. 不过,最近,我看过博客帖子等,建议编写一个控制台应用程序,然后使用计划任务来执行它。

I have read many posts talking to this very issue, but have not seen a definitive answer about which process is better. 我已经阅读了很多关于这个问题的帖子,但是没有看到关于哪个过程更好的明确答案。

What do any of you think? 你们有什么想法?

Thanks for any thoughts. 谢谢你的任何想法。

If it is a one per week application, why waste the resources for it to be running in the background for the rest of the week. 如果它是每周一次的应用程序,为什么要浪费资源让它在一周的其余时间在后台运行。

A console application seems much more appropriate. 控制台应用程序似乎更合适。

The typical rule of thumb that I use is something along these lines. 我使用的典型经验法则是沿着这些方向。 First I ask a few questions. 首先我问几个问题。

  1. Frequency of Execution 执行频率
  2. Frequency of changes to #1 变更频率为#1
  3. Triggering Mechanism 触发机制

Basically from here if the frequency of execution is daily or less frequent I'll almost always lean towards a scheduled task. 基本上从这里开始,如果执行频率是每天或更低频率,我几乎总是倾向于计划任务。 Then looking at the frequency for changes, if there is a high demand for schedule changes, I'll also try to lean towards scheduled tasks, to allow no-coding changes for schedule changes. 然后查看更改的频率,如果对计划更改有很高的要求,我还会尝试倾向于计划任务,以允许对计划更改进行无编码更改。 lastly if there is ever a thought of a trigger other than time, then I'll lean towards windows services to help "future proof" an application. 最后,如果曾经想过除时间之外的触发器,那么我将倾向于Windows服务以帮助“将来证明”一个应用程序。 Say for example the requirement changes to be run every time a user drops a file in X folder. 比如说每次用户在X文件夹中删除文件时都要运行需求更改。

The basic rule I follow is: if you need to be running continuously because events of interest can happen at any time, use a service (or daemon in UNIX). 我遵循的基本规则是:如果您需要连续运行,因为感兴趣的事件可能随时发生,请使用服务(或UNIX中的守护程序)。

If you just want to periodically do something, use a scheduled task (or cron ). 如果您只是想要定期执行某些操作,请使用计划任务(或cron )。

The clincher here is your phrase "must be run once a week at a particular time" - go for a scheduled task. 这里的关键是你的短语"must be run once a week at a particular time" - 去执行预定的任务。

如果您只有一个应用程序,并且您需要每周运行一次,那么调度程序将会很好,因为不需要在系统上运行单独的服务和进程,该系统将在大多数时间处于空闲状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM