Windows服务关闭 [英] Windows service shutdown

查看:90
本文介绍了Windows服务关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,我需要睡觉大约10-20

分钟。


问题是,如果你试图在这段时间关闭它,它会告诉你该服务没有及时响应并最终处于停止状态,并且有一个b $ b状态在你重新启动系统之前你无能为力。


我为解决问题所做的只是在我的关机方法中设置一个标志

并设置一个循环在我的程序中一次进入睡眠状态1分钟,唤醒
并检查标志然后再睡一分钟直到最大值

分钟数已经过了。


我不知道服务在获得该消息之前必须关闭
的持续时间是多少,或者在某个地方有设置

你可以设置吗?


谢谢,


Tom

I have a Windows Service that I need to put to sleep for about 10-20
minutes.

The problem is that if you try to shut it down during this time, it will
tell you that the service didn''t respond in time and end up in a "Stopping"
state and there is nothing you can do until you reboot the system.

What I did to solve the problem was just set a flag in my Shutdown method
and set up a loop in my program to go to sleep for 1 minute at a time, wake
up and check the flag then go to sleep for another minute until the maximum
number of minutes have elapsed.

What I don''t know is what is the duration of time that a service has to
shutdown before it gets that message, or is there a setting somewhere where
you can set it?

Thanks,

Tom

推荐答案

而不是cal ling Sleep(x)使用AutoResetEvent.WaitOne(x,false)。


这样它会为x睡觉但是如果你设置了事件则立即醒来。

然后您可以从关机方法调用MyAutoResetEvent.Set()并立即唤醒

线程。


-

Pete

====
http:// mrpmorris .blogspot.com
http://www.capableobjects.com

Instead of calling Sleep(x) use AutoResetEvent.WaitOne(x, false).

This way it will sleep for x but wake up immediately if you set the event.
You can then call MyAutoResetEvent.Set() from your shutdown method and the
thread will awaken immediately.

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com


2008年10月10日星期五08:54:55 -0700,tshad< tf*@dslextreme.comwrote:
On Fri, 10 Oct 2008 08:54:55 -0700, tshad <tf*@dslextreme.comwrote:

[...]

我不知道服务的持续时间是多少
在收到该消息之前关闭,或者某个地方有设置

哪里

你可以设置吗?
[...]
What I don''t know is what is the duration of time that a service has to
shutdown before it gets that message, or is there a setting somewhere
where
you can set it?



您应该修复

服务,这样它就不会变得反应迟钝。正在睡觉(为什么

你认为它有必要睡觉约10-20分钟很难

也明白了,但暂时让我们'我认为'实际上是必要的

,即使它看起来不太正确)。


从你的描述中看出来(这是不可能的)要知道没有一个

简洁但完整的代码示例),通常

处理该服务的消息,你正在调用类似

Thread.Sleep()带有一些长值。当然,这导致服务器

无法响应_any_消息。


而不是阻止该线程,你应该只是设置一些状态

变量,表示服务处于休眠状态。州。在设置该标志时进入的任何

请求可以被忽略,拒绝,

等等,但你仍然会适当地回应关闭

请求。在你设置标志的同时,启动一个计时器,当计时器周期结束时,它将清除标志。


这样,你将使您的服务响应,甚至可以在这个睡眠中间正确关闭
。状态,但是

仍然没有响应你想要暂停的行动。

睡眠状态状态。


Pete

Rather than worrying about that specific duration, you should just fix the
service so that it doesn''t become unresponsive while it''s sleeping (why
you feel it necessary for it to sleep "for about 10-20 minutes" is hard to
understand too, but for the moment let''s assume that''s actually necessary
even though it doesn''t seem quite right).

From your description (it''s impossible to know for sure without a
concise-but-complete code sample) in the thread that would normally
process messages for the service, you are calling something like
Thread.Sleep() with some long value. That, of course, causes the server
to fail to respond to _any_ message.

Instead of blocking that thread, you should simply be setting some state
variable that indicates that the service is in its "sleeping" state. Any
requests that come in while that flag is set can be ignored, rejected,
etc. as appropriate but you would still respond appropriately to shutdown
requests. At the same time that you set the flag, start a timer that will
clear the flag when the timer period has elapsed.

In that way, you will leave your service responsive, and it can even
shutdown properly in the middle of this "sleeping" state, but it will
still not respond to actions that you want to be suspended during the
"sleeping" state.

Pete


我认为这是服务暂停的原因,CanPauseAndContinue和

OnPause事件,假设你有代码在某个地方告诉服务暂停,然后继续。


-

Phil Wilson

Windows Installer权威指南
http://www.apress.com/book/view/1590592972

" tshad" < tf*@dslextreme.com写信息

新闻:ey ************** @ TK2MSFTNGP04.phx.gbl ...
I think this is what service pause is for, CanPauseAndContinue and the
OnPause event, assuming you have code somewhere to tell the service to
pause, and continue later.

--
Phil Wilson
Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972
"tshad" <tf*@dslextreme.comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...

>我有一个Windows服务,我需要睡觉大约10-20
分钟。


问题是如果你试图在这段时间内将其关闭,它会告诉你该服务没有及时响应并最终以b
停止 ;在重启

系统之前,你无能为力。


我为解决问题所做的只是在我的Shutdown方法中设置了一个标志

并在我的程序中设置一个循环,一次睡1分钟,

醒来并检查标志然后再睡一分钟,直到

已经过去的最大分钟数。


我不知道服务的持续时间是多长时间

在收到该消息之前关闭,或者在某个地方有设置

你可以设置它吗?


谢谢,


Tom
>I have a Windows Service that I need to put to sleep for about 10-20
minutes.

The problem is that if you try to shut it down during this time, it will
tell you that the service didn''t respond in time and end up in a
"Stopping" state and there is nothing you can do until you reboot the
system.

What I did to solve the problem was just set a flag in my Shutdown method
and set up a loop in my program to go to sleep for 1 minute at a time,
wake up and check the flag then go to sleep for another minute until the
maximum number of minutes have elapsed.

What I don''t know is what is the duration of time that a service has to
shutdown before it gets that message, or is there a setting somewhere
where you can set it?

Thanks,

Tom



这篇关于Windows服务关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆