简体   繁体   English

处理onStop事件

[英]Handling onStop event

如何在C#中创建的Windows服务中处理onStop事件。

What is it you want to do? 您想做什么? If you are fine with your service just stopping, do nothing. 如果您对服务感到满意,只是停止就可以了,什么也不做。 You don't need to handle it to actually get the service to stop - it is there to allow you to do stuff when the service receives the stop notification from Windows. 您不需要处理它即可使服务真正停止-在服务收到Windows发出的停止通知时,您可以在其中进行操作。

In your class that derives from ServiceBase , you need to override the OnStop method: 在从ServiceBase派生的类中,您需要重写OnStop方法:

protected override void OnStop()
{}

You can then put your logic that should be executed when the service is stopping in there. 然后,您可以将在服务停止时应执行的逻辑放在其中。

Note that Windows allows a short time frame for a service to stop (around 30 seconds I think) - after this it will report that the service cannot stop. 请注意,Windows允许服务停止的时间很短(我认为大约30秒)-在此之后它将报告服务无法停止。 This means you cannot do anything too lengthy in the OnStop method. 这意味着您不能在OnStop方法中执行任何冗长的OnStop It is usually useful to log that your service received the stop event. 记录您的服务收到停止事件通常很有用。

Just have OnStop method overriden: 只是重写OnStop方法:

protected override void OnStop()
{
    // Your code goes here.
}

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

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