Stop Windows Service and avoid time out warning message

谁都会走 提交于 2019-12-11 19:36:09

问题


I have implemented a windows servcice which is intended to start a business process in a scheduled manner. Once the buisiness process is started, it shouldn't be terminated by stopping the service. Hence I used the following OnStop method:

    protected override void OnStop()
    {
        /*
         * Check, whether the business process is currently running; 
         */ 
        if (_sh.IsBusinessProcessRunning())
        {
            eventLog1.WriteEntry(String.Format(
                "Stop instruction received! Service will be stopped, as soon the process '{0}' has completed. ", 
                ServiceHelper._businessExecutableName));
        }
        while (_sh.IsBusinessProcessRunning())
        {
            // Check each 30 seconds, whether the business process is still running
            Thread.Sleep(30000);
        }

        eventLog1.WriteEntry("Service stopped!");
    }

Actually this works fine, except that I'm getting the time out error dialog which may confuse the customer. Is there any way to modify the message within this dialog or to avoid it from getting displayed? (I would prefer to update the message, if possible)


Message:

The service "..." on "localhost" couldn't be terminated. The service did not respond to the start or control request in a timely fashion.


回答1:


You're sleeping for 30 seconds but Windows usually only gives ~12 seconds in which to shutdown the service so you're easily surpassing that, which causes that dialog to pop up.

You can request addition time by calling ServiceBase.RequestAdditionalTime which should tell the SCM that your service is still responding it's just busy.

I also wouldn't use Thread.Sleep for such a large block of time. Reduce that to a couple of seconds, it's in a loop anyhow.



来源:https://stackoverflow.com/questions/21675625/stop-windows-service-and-avoid-time-out-warning-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!