简体   繁体   English

关闭Windows服务中的长时间运行的进程

[英]Shutting down a Long-running process in a Windows Service

I have a Windows Service that performs a long-running process. 我有一个Windows服务,它执行一个长时间运行的过程。 It is triggered by a timer and the entire process can take a few minutes to complete. 它由计时器触发,整个过程可能需要几分钟才能完成。 When the timer elapses the service instantiates a management object that performs the various tasks, logs the results and then exits. 计时器结束后,服务会实例化执行各种任务的管理对象,记录结果,然后退出。

I have not implemented anything to handle those occasions when the server is shutdown during the middle of the process. 在过程中途服务器关闭的情况下,我还没有实现任何处理方法。 It could cause some problems. 这可能会引起一些问题。 What is the best practice to handle this? 处理此问题的最佳做法是什么?

Can only give vague suggestions since I don't know what task you are actually doing. 由于我不知道您实际上正在执行什么任务,因此只能给出模糊的建议。

If it is something to do w/ database, there is transaction that can be rolled back if it is not committed. 如果要使用数据库执行某些操作,那么如果未提交,则可以回滚事务。

If it involves some file manipulation, perhaps take a look at this article on Transactional NTFS. 如果涉及到一些文件处理,也许看看这个文章在事务性NTFS。 You can use it in combination w/ TransactionScope object to ensure atomic transaction. 您可以结合使用它与TransactionScope对象来确保原子事务。

If you are dealing with web services, well the service boundary will dictate when one transaction starts / ends and when the other one begins, use compensation model (if you break something on your part, you need to provide a way later on, after recovery, a way to notify / execute compensation scripts on the other end. (Think about ordering book online and how to handle backorder, cancellation, etc.) 如果您正在处理Web服务,那么服务边界将决定一项事务开始/结束的时间,而另一项事务开始的时间,使用补偿模型(如果您破坏了一部分,则需要在恢复之后提供一种方法) ,一种在另一端通知/执行补偿脚本的方式(考虑在线订购图书以及如何处理延期交货,取消订单等)

For tracking mechanism, log every steps and the timelines for troubleshooting if something like shutdown occurs. 对于跟踪机制,如果发生诸如关机之类的日志,请记录每个步骤和时间表以进行故障排除。

If your describing essentially a batch process its ok to have a timer that does work at an interval - much of the world works that way. 如果您实质上描述了一个批处理过程,则可以有一个间隔运行的计时器是可以的-世界上很多地方都是这样工作的。

If its long running, try to keep your units of work, or batches, small enough that your process can at least check to see if its been signaled to stop or not. 如果它长期运行,请尝试将工作单元或批次保持足够小,以使您的过程至少可以检查以查看是否已发出停止信号。 This will allow the service to exit gracefully instead of essentially ignoring the service stop message. 这将允许服务正常退出,而不是实质上忽略服务停止消息。

Somewhere in your timer function you have a property, IsShutdownRequired or some such, that your checking (assuming some loop processing). 在您的计时器函数中的某处,您具有一个属性IsShutdownRequired或诸如此类的属性(可以进行一些循环处理)进行检查。 This property is set to true in the service stop control message, which allows your process to gracefully exit by either not trying to do more work, or as Jimmy suggested, rolling back that work if in a transaction. 该属性在服务停止控制消息中设置为true,它使您的进程可以通过不尝试做更多的工作来正常退出,或者按照吉米的建议,在事务中回退该工作。

Ideally, smaller batches would be better than one big one. 理想情况下,小批量将比大批量更好。

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

相关问题 关闭Windows服务时将cmd参数传递给正在运行的进程 - Pass cmd argument to running process when shutting down windows service 在Windows服务中运行长时间运行的任务 - Running a long-running Task within a Windows Service 从Windows服务处理长时间运行的操作 - Processing long-running operations from a windows service 显示长时间运行过程的进度 - Show progress on long-running process 长期运行的WCF服务即将终止的原因是什么? - Reasons for long-running WCF service dying? 创建长期运行的工作流服务 - Creating a long-running Workflow Service 如何在Windows服务中运行长时间运行的进程? - how to run a long running process in a windows service? Windows Service Application仅执行一次长时间运行的任务,并在完成后自行停止 - Windows Service Application to perform long-running task only once and stop itself after completion 在长期运行的Windows服务中进行依赖注入-组合根源是正确的主意吗? - Dependency injection in long-running Windows service - Composition root the right idea? 在Winforms中显示模式加载窗口以长时间运行 - Show modal loading window in Winforms for long-running process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM