为什么在Windows Service停止时调用base.OnStop()? [英] Why call base.OnStop() when Windows Service is stopped?

查看:37
本文介绍了为什么在Windows Service停止时调用base.OnStop()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个C#.Net Windows服务,想知道是否总是需要在该服务的 OnStop()方法中调用 base.OnStop(); 为什么?

I'm creating a C#.Net Windows Service and am wondering if you always have to call base.OnStop(); in the service's OnStop() method and why?

protected override void OnStop()
{
    threadRunning = false;

    this.ExitCode = 0;
    base.OnStop();
}

推荐答案

来自

OnStop预计将在派生类.为了服务很有用,OnStart和OnStop应该两者都在您的服务中实现课.

OnStop is expected to be overridden in the derived class. For the service to be useful, OnStart and OnStop should both be implemented in your service class.

因此,它实际上用作停止服务的通知.

So it's really used as a notification to your service that it is stopping.

浏览一下ServiceBase.cs的.NET源代码,发现该方法无济于事:

A glance at the .NET source code for ServiceBase.cs reveals the method does nothing:

protected virtual void OnStop()
{
}

那么,您需要调用它吗?否.但是这样做仍然是一个很好的形式.基本实现可能有一天会更改.

So, do you need to call it? No. But it is still good form to do so. The base implementation may change someday.

这篇关于为什么在Windows Service停止时调用base.OnStop()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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