0

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.

My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
  • Can you translate the message to english please :) – OuSs Feb 10 '14 at 11:17
  • You need to find a way to preserve the state of that business process and exit. Your process might get ripped out from under you if you refuse to shut down when the OS requests you to. And creating "unkillable" services starts to enter the realm of malware. – Damien_The_Unbeliever Feb 10 '14 at 11:21
  • 1
    Also, of course, if someone can attempt to stop your service, they can also almost certainly force the machine to shut down or reboot - since you'll need to cope with *that* situation in a way that doesn't involve indefinitely postponing shutdown, use whatever strategy you devise to cope with that to deal with `Stop` also. – Damien_The_Unbeliever Feb 10 '14 at 11:35

1 Answers1

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.

Lloyd
  • 29,197
  • 4
  • 84
  • 98