简体   繁体   English

以编程方式授予/拒绝Windows服务的onStop功能

[英]Grant/deny onStop feature of a windows service programmatically

I know that I can deny onStop feauture by setting CanStop option to false in Service's properties. 我知道可以通过在Service属性中将CanStop选项设置为false来拒绝onStop功能。 This is not what I want cause this will permanently deny onStop capabilities. 这不是我想要的,因为这将永久拒绝onStop功能。

What I want is to grant/deny stop capabilities programmatically. 我想要的是通过编程方式授予/拒绝停止功能。 My service lifecycle is pretty simple: 我的服务生命周期非常简单:

starts => { run some action => sleeps for 2 minutes } x nTimes => stop

What I would is to deny stop capabilities when service is in action and grant that feature when service is idle (ie If user try to stop it when not permitted nothing happen, else the service really stop itself). 我要做的就是在服务运行时拒绝停止功能,而在服务空闲时授予该功能(即,如果用户在不允许的情况下尝试停止它,则该服务实际上会自行停止)。

This is how my service is written, I have various way to understand if is idle. 这就是我的服务编写方式,我有多种方式来了解是否空闲。

partial class Service : ServiceBase
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private DateTime _lastRun = DateTime.Now;
        private System.Timers.Timer _timer = new System.Timers.Timer(Convert.ToInt32(ConfigurationManager.AppSettings["pollingInterval"]) * 60 * 1000);

        public Service()
        {
            InitializeComponent();
        }

        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _timer.Enabled = false;
            DateTime now = DateTime.Now;
            if (now.Minute > _lastRun.Minute)
            {
                ClientVS cvs = new ClientVS();
                cvs.run();
            }
            _lastRun = now;
            _timer.Enabled = true;
        }

        protected override void OnStart(string[] args)
        {
            log.Info("Started");
            _timer.Elapsed += timer_Elapsed;
            _timer.Start();
        }

        protected override void OnStop()
        {
            log.Info("Stopped");
        }
    }

Don't do this. 不要这样 Service must stop promptly (5 seconds or less) when requested to stop. 当要求停止时,服务必须迅速停止(5秒或更短)。 If you delay or deny stop requests from SCM, you are guaranteed to annoy your users when they reboot computer. 如果您延迟或拒绝来自SCM的停止请求,则可以确保在用户重新启动计算机时使他们烦恼。 You can potentially change service configuration dynamically, but it is probably bad idea, because changing serice config frequently does not sound right. 您可以潜在地动态更改服务配置,但这可能不是一个好主意,因为频繁更改serice config听起来不正确。

Semantics of the service STOP command is just this -- service has to stop at the earliest safe place. service STOP命令的语义就是这样-服务必须在最早的安全位置停止。 Your action is different than that. 您的动作与此不同。 What I would recommend is to implement a custom mechanism that would represent your semantics exactly (that is, do NOT reuse STOP command). 我建议的是实现一种自定义机制,该机制将准确地表示您的语义(即,不要重用STOP命令)。 There are couple of way to do this: 有几种方法可以做到这一点:

  1. Any sort of IPC mechanism. 任何种类的IPC机制。 A named event would probably be easiest to implement. 命名事件可能最容易实现。 A service will create a named event and wait for it in one of the background threads. 服务将创建一个命名事件,并在后台线程之一中等待它。 When event is set (by your user mode application), it would do necessary checks and would call ServiceBase.Stop conditionally. 设置事件后(由用户模式应用程序设置),它将进行必要的检查并有条件地调用ServiceBase.Stop

  2. Invent a custom service control command. 发明定制服务控制命令。 This is easy to do, just override ServiceBase.OnCustomCommand , make up an integer between 128 and 255, that would represent your command, and implement OnCustomCommand to call ServiceBase.Stop conditionally. 这很容易做到,只需覆盖ServiceBase.OnCustomCommand ,将其表示为128到255之间的整数即可,即可表示您的命令,并实现OnCustomCommand有条件地调用ServiceBase.Stop To send custom command to service you can use API or sc control command. 要将自定义命令发送到服务,可以使用API​​或sc control命令。 Assuming you made up 234 as you new command: 假设您组成234个新命令:

sc control MyService 234

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

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