简体   繁体   English

覆盖Windows服务中的Stop

[英]Overriding Stop in a Windows Service

I have this windows service class here written in C#. 我这里有用C#编写的Windows服务类。 It has a member function called Stop but apparently its base class ServiceBase also has a Stop function. 它具有一个称为Stop的成员函数,但显然它的基类ServiceBase也具有Stop函数。

ServiceBase.Stop() calls OnStop which is overriden in the service again which calls this.Stop() in its own object again. ServiceBase.Stop()调用OnStop ,该服务在服务中再次被覆盖,该服务再次在其自己的对象中调用this.Stop()

Now VS gives the warning: Server.Stop() hides inherited member System.ServiceProcess.ServiceBase.Stop() . 现在,VS发出警告: Server.Stop()隐藏继承的成员System.ServiceProcess.ServiceBase.Stop() Use the new keyword if hiding was intended 如果打算进行隐藏,请使用new关键字

Can I just use new without changing the behaviour of the service? 我可以只使用new而不更改服务行为吗?

If you aren't intending to override the base Stop() method, why don't you just rename it to avoid any ambiguity? 如果您不打算覆盖基本Stop()方法,为什么不重命名它以避免任何歧义?

If you want to do something before or after the service controller causes Stop() to be called, override it - and then call base.Stop() either before or after your own code as required. 如果要在服务控制器导致调用Stop()之前或之后执行某些操作,请覆盖它-然后根据需要在自己的代码之前或之后调用base.Stop()

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

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