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

发布于 2024-07-12 08:58:36 字数 418 浏览 7 评论 0原文

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

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

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

你有什么意见? 有一个空闲的服务是否可能并且很好,或者您会推荐任务计划程序吗?

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.

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)

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最美不过初阳 2024-07-19 08:58:36

您可能会发现这个 视频很有趣。 对负责 Windows 服务的工程师进行了很好的采访,并讨论了何时选择服务或任务。 简而言之,如果您的功能属于定期维护类别,请执行任务。

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.

柏林苍穹下 2024-07-19 08:58:36

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

此外,如果开发人员“忘记”执行诸如关闭连接、文件等随着时间的推移而积累的操作,那么连续运行和“睡眠”的实用程序就有引发问题的风险。 让程序“运行并退出”是一个额外的安全毯。 :-)

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. :-)

月下客 2024-07-19 08:58:36

Windows 服务更加安全:没有人可以放弃它并且永远可以工作。 我发现 Windows 任务存在很多问题(无法执行,...)。

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

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 Task Scheduler is for end-user tasks not for appplication tasks. Anyway, Windows Backup uses it ;-)

著墨染雨君画夕 2024-07-19 08:58:36

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

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

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

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.

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.

护你周全 2024-07-19 08:58:36

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

对于最近的项目,我使用 Quartz.NET 在 Windows 服务中设置计划处理。 实际上设置起来并不太难(很好的教程),并且 CronTrigger 只需很少的配置即可为您提供大量的灵活性。 与原始的 .NET 计时器相比,我更喜欢它。

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.

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.

花开半夏魅人心 2024-07-19 08:58:36

我想投入我的 5 美分,当你的任务没有运行时,使用任务调度程序会节省一些内存。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文