简体   繁体   English

长期运行的Windows服务

[英]Long running Windows Services

Folks, 民间,

I want to develop a long running windows service (it should be working without problems for months), and I wonder what is the better option here: 我想开发一个长期运行的Windows服务(它应该可以正常工作数月之久),我想知道这里有什么更好的选择:

  1. Use a while(true) loop in the OnStop method 在OnStop方法中使用while(true)循环
  2. Use a timer to tick each n seconds and trigger my code 使用计时器每隔n秒滴答一次并触发我的代码
  3. Any other options ? 还有其他选择吗?

Thanks Essam 谢谢伊萨姆

I wouldn't do #1. 我不会做#1。

I'd either do #2, or I'd spin off a separate thread during OnStart that does the actual work. 我要么做#2,要么在执行实际工作的OnStart期间剥离一个单独的线程。

Anything but #1 除了#1之外的任何东西
The services manager (or the user, if he's the one activating the controls) expects OnStart() and OnStop() to return in a timely fashion. 服务经理(或用户,如果他是激活控件的用户)则希望OnStart()OnStop()及时返回。
The way it's usually done is to start your own thread that keeps things running and ofcourse, listens to an event that might tell it to stop. 通常,这样做的方法是启动自己的线程,以保持运行正常,并监听可能会使其停止的事件。

Might be worth considering a scheduled task with a short interval. 可能值得考虑短间隔的计划任务。 Saves writing a lot of plumbing code and dealing with the peculiarities of Windows Services timers . 节省了编写大量管道代码和处理Windows Services计时器的特殊性。

Don't mess with the service controller code. 不要弄乱服务控制器代码。 If the service wants to stop, you will only make matters worse by using #1. 如果服务要停止,则只能使用#1使情况更糟。 And BTW the service can always crash , in which case your while(true) won't help you a thing. 而且顺便说一句,该服务可能总是崩溃 ,在这种情况下,您的while(true)不能为您提供任何帮助。

If you really want to have a " running windows service (it should be working without problems for months) ", you'd better make sure your own code is properly and thoroughly tested using unit and integration tests before your run it as a service. 如果您真的想拥有一个“ 正在运行Windows服务(它应该可以正常工作数月之久) ”,那么作为服务运行之前 ,最好确保已使用单元测试和集成测试您自己的代码进行了适当且全面的测试

I would NOT recommend #1. 我不会推荐#1。

What I've done in the past for the exact same scenario/situation is create a scheduled task that runs ever N seconds, kicks off a small script that simply does these 2 things: #1 checks for “IsAlreadyRunning” flag (which is read from the database) #2 If the flag is true, then the script immediately stops end exits. 我过去为完全相同的场景/情况所做的工作是创建一个计划的任务,该任务每N秒运行一次,启动一个简单的脚本,该脚本仅执行以下两项操作:#1检查“ IsAlreadyRunning”标志(已读取) #2如果该标志为true,则脚本立即停止结束出口。 If the flag is false, the script kicks off a separate process (exe) in a new thread (which utilizes a service to perform a task that can be either very short or sometimes really long, depending on the amount of records to process). 如果该标志为假,则脚本将在新线程中启动一个单独的进程(exe)(该线程利用服务来执行可能非常短或有时甚至很长的任务,具体取决于要处理的记录量)。 This process of course sets and resets the IsAlreadyRunning flag to ensure threads do not kick off actions that overlap. 当然,此过程将设置并重置IsAlreadyRunning标志,以确保线程不会启动重叠的动作。 I have a service that's been running for years now with this approach and I never had any problems with it. 我使用这种方法已经运行了多年的服务,但是我从来没有遇到任何问题。 My main process utilizes a web service and bunch of other things to perform some heavy backup operations. 我的主要流程是利用Web服务和许多其他东西来执行一些繁重的备份操作。

System.Threading.Timer类似乎适合这种用法。

Is it doing a 它在做一个

1 clean up task, or 1个清理任务,或者

2 waking up and looking to see if needs to run a task 2醒来,看看是否需要运行任务

If it is something like #2, then using MSMQ would be more appropriate. 如果它是#2之类的东西,那么使用MSMQ更合适。 With MSMQ task would get done almost immediately. 使用MSMQ,任务将立即完成。

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

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