用于维护任务的Windows服务或任务计划程序?

时间:2022-04-21 05:23:53

I have a C# Application that does some maintenance tasks. It needs to run roughly every hour, although it's not too important if it's a bit off. And it has to run on a Win2003 Server with no one logged in.

我有一个C#应用程序来执行一些维护任务。它需要大约每小时运行一次,但如果它有点偏差则不太重要。它必须在没有用户登录的Win2003服务器上运行。

Basically I wonder if I should write a Windows Service, and if yes, if Thread.Sleep() is a proper way to pause the Thread for an hour, or if there are better ways to make sure the Thread stays idle and does not put any server load? (aka. what is the most opposite of a Spinlock)

基本上我想知道我是否应该写一个Windows服务,如果是的话,如果Thread.Sleep()是一个暂停线程一小时的正确方法,或者有更好的方法来确保线程保持空闲并且不放任何服务器负载? (又名。与Spinlock最相反的是什么)

The alternative is the Windows Task Scheduler, but I am not sure if that is good for server use since a) i'd have to store the schedule in it rather than in my app.config, b) I cannot easily control the service through start/stop/restart and c) I do not know if the Credentials of the Windows User are as secure as when I enter it in the Service MMC Snapin.

替代方案是Windows任务计划程序,但我不确定这是否适合服务器使用,因为a)我必须将计划存储在其中而不是我的app.config中,b)我无法通过以下方式轻松控制服务启动/停止/重启c)我不知道Windows用户的凭据是否与我在Service MMC Snapin中输入时一样安全。

What are your opinions? Is it possible and good to have an idle Service or would you recommend the task scheduler instead?

你有什么看法?拥有空闲服务是否可能有用,或者您是否建议使用任务调度程序?

6 个解决方案

#1


You may find this video interesting . Good interview with the engineer responsible for windows services and also goes into when to pick service or task. In a nutshell if your feature falls into the periodic maintenance category go with a task.

您可能会发现此视频很有趣。对负责Windows服务的工程师的良好访谈,以及何时选择服务或任务。简而言之,如果您的功能属于定期维护类别,请执行任务。

#2


I prefer the Task Scheduler myself, as it is easier to maintain and make changes to the schedule if you need to.

我自己更喜欢任务计划程序,因为如果需要,它更容易维护和更改计划。

Also, utilities that run continuously and "sleep" run the risk of causing problems if the developer "forgets" to do things like close connections, files etc. which can build up over time. Having the program "run and exit" is an extra safety blanket. :-)

此外,如果开发人员“忘记”执行诸如关闭连接,文件等可能随着时间的推移而积累的事情,那么连续运行并“休眠”的实用程序会冒着导致问题的风险。让程序“运行和退出”是一个额外的安全毯。 :-)

#3


Windows service is more secure: no one can drop it and ever works. I've found a lot of troubles with Windows Tasks (not executing, ...).

Windows服务更安全:没有人可以放弃它并且永远有效。我发现Windows任务有很多麻烦(没有执行,......)。

Windows Task Scheduler is for end-user tasks not for appplication tasks. Anyway, Windows Backup uses it ;-)

Windows任务计划程序适用于最终用户任务,不适用于应用程序任务。无论如何,Windows备份使用它;-)

#4


If you go the services route then I would recommend using System.Threading.Timer. With that you can set it to fire an event once an hour. You can put the interval in the app.config if you think you will ever need to change it.

如果你去服务路线,那么我建议使用System.Threading.Timer。有了它,您可以将其设置为每小时触发一次事件。如果您认为需要更改它,可以将时间间隔放在app.config中。

Services also run under the local system account (by default) whereas you must supply a username/password when using scheduled tasks. If you use your username it becomes a maintenance issue if you ever change your password and forget to update the task.

服务也在本地系统帐户下运行(默认情况下),而在使用计划任务时必须提供用户名/密码。如果您使用用户名,如果您更改密码并忘记更新任务,则会成为维护问题。

I have a situation where I use a combination of both actually. The service runs constantly during the day but they do network and database maintenance every night. So, I use two scheduled tasks. One to shut the service down at midnight and one to turn it back on at 4am. This is done by calling .BAT files with NET STOP/ NET START commands.

我有一种情况,我实际上使用两者的组合。该服务在白天不断运行,但每晚都进行网络和数据库维护。所以,我使用两个计划任务。一个在午夜关闭服务,一个在凌晨4点重新开启。这是通过使用NET STOP / NET START命令调用.BAT文件来完成的。

#5


I think a windows service is probably more reliable and easier to monitor, but it won't offer the same scheduling flexibility as a scheduled task out-of-the-box. I'd probably go with the service anyway, since most enterprisey apps tend to run as services, not scheduled tasks.

我认为Windows服务可能更可靠,更容易监控,但它不会提供与开箱即用的计划任务相同的调度灵活性。我可能会继续使用该服务,因为大多数企业应用程序往往作为服务运行,而不是计划任务。

For recent projects, I've used Quartz.NET to setup scheduled processing in windows services. It's actually not too hard to setup (good tutorial), and the CronTrigger will give you a ton of flexibility with only a tiny bit of configuration. I like it better than a raw .NET Timer.

对于最近的项目,我使用Quartz.NET来设置Windows服务中的计划处理。它实际上并不太难设置(好的教程),而CronTrigger只需要一点点配置就能给你很大的灵活性。我比原始的.NET Timer更喜欢它。

#6


I suppose to throw my 5 cents worth in, using task scheduler will save some memory when your task is not running.

我想要投入5美分,使用任务调度程序将在您的任务未运行时节省一些内存。

#1


You may find this video interesting . Good interview with the engineer responsible for windows services and also goes into when to pick service or task. In a nutshell if your feature falls into the periodic maintenance category go with a task.

您可能会发现此视频很有趣。对负责Windows服务的工程师的良好访谈,以及何时选择服务或任务。简而言之,如果您的功能属于定期维护类别,请执行任务。

#2


I prefer the Task Scheduler myself, as it is easier to maintain and make changes to the schedule if you need to.

我自己更喜欢任务计划程序,因为如果需要,它更容易维护和更改计划。

Also, utilities that run continuously and "sleep" run the risk of causing problems if the developer "forgets" to do things like close connections, files etc. which can build up over time. Having the program "run and exit" is an extra safety blanket. :-)

此外,如果开发人员“忘记”执行诸如关闭连接,文件等可能随着时间的推移而积累的事情,那么连续运行并“休眠”的实用程序会冒着导致问题的风险。让程序“运行和退出”是一个额外的安全毯。 :-)

#3


Windows service is more secure: no one can drop it and ever works. I've found a lot of troubles with Windows Tasks (not executing, ...).

Windows服务更安全:没有人可以放弃它并且永远有效。我发现Windows任务有很多麻烦(没有执行,......)。

Windows Task Scheduler is for end-user tasks not for appplication tasks. Anyway, Windows Backup uses it ;-)

Windows任务计划程序适用于最终用户任务,不适用于应用程序任务。无论如何,Windows备份使用它;-)

#4


If you go the services route then I would recommend using System.Threading.Timer. With that you can set it to fire an event once an hour. You can put the interval in the app.config if you think you will ever need to change it.

如果你去服务路线,那么我建议使用System.Threading.Timer。有了它,您可以将其设置为每小时触发一次事件。如果您认为需要更改它,可以将时间间隔放在app.config中。

Services also run under the local system account (by default) whereas you must supply a username/password when using scheduled tasks. If you use your username it becomes a maintenance issue if you ever change your password and forget to update the task.

服务也在本地系统帐户下运行(默认情况下),而在使用计划任务时必须提供用户名/密码。如果您使用用户名,如果您更改密码并忘记更新任务,则会成为维护问题。

I have a situation where I use a combination of both actually. The service runs constantly during the day but they do network and database maintenance every night. So, I use two scheduled tasks. One to shut the service down at midnight and one to turn it back on at 4am. This is done by calling .BAT files with NET STOP/ NET START commands.

我有一种情况,我实际上使用两者的组合。该服务在白天不断运行,但每晚都进行网络和数据库维护。所以,我使用两个计划任务。一个在午夜关闭服务,一个在凌晨4点重新开启。这是通过使用NET STOP / NET START命令调用.BAT文件来完成的。

#5


I think a windows service is probably more reliable and easier to monitor, but it won't offer the same scheduling flexibility as a scheduled task out-of-the-box. I'd probably go with the service anyway, since most enterprisey apps tend to run as services, not scheduled tasks.

我认为Windows服务可能更可靠,更容易监控,但它不会提供与开箱即用的计划任务相同的调度灵活性。我可能会继续使用该服务,因为大多数企业应用程序往往作为服务运行,而不是计划任务。

For recent projects, I've used Quartz.NET to setup scheduled processing in windows services. It's actually not too hard to setup (good tutorial), and the CronTrigger will give you a ton of flexibility with only a tiny bit of configuration. I like it better than a raw .NET Timer.

对于最近的项目,我使用Quartz.NET来设置Windows服务中的计划处理。它实际上并不太难设置(好的教程),而CronTrigger只需要一点点配置就能给你很大的灵活性。我比原始的.NET Timer更喜欢它。

#6


I suppose to throw my 5 cents worth in, using task scheduler will save some memory when your task is not running.

我想要投入5美分,使用任务调度程序将在您的任务未运行时节省一些内存。