问题
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