33

I'm trying to stop a Windows service on a local machine (the service is Topshelf.Host, if that matters) with this code:

serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

timeout is set to 1 hour, but service never actually gets stopped. Strange thing with it is that from within Services MMC snap-in I see it in "Stopping" state first, but after a while it reverts back to "Started". However, when I try to stop it manually, an error occurs:

Windows could not stop the Topshelf.Host service on Local Computer.
Error 1061: The service cannot accept control messages at this time.

Am I missing something here?

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288

9 Answers9

66

I know I am quite late to answer this but I faced a similar issue , i.e., the error: "The service cannot accept control messages at this time." and would like to add this as a reference for others.

You can try killing this service using powershell (run powershell as administrator):

#Get the PID of the required service with the help of the service name, say, service name.
$ServicePID = (get-wmiobject win32_service | where { $_.name -eq 'service name'}).processID 

#Now with this PID, you can kill the service
taskkill /f /pid $ServicePID
Rishi
  • 980
  • 10
  • 21
  • I could not kill the the service using Task Manager as an administrator, and rebooting did not help either. This was the method that worked for me. – ChrisG Aug 18 '17 at 11:36
  • 2
    Worked for me. Don't forget to run powershell as administrator also. – Aaron Oct 13 '17 at 23:03
  • I used task manager to find the service process by service name instead of powershell script. – Jackson Jul 23 '21 at 13:35
  • @Jackson ... Yes, you can find both the process as well as the process ID on the task manager. But there are scenarios where, an associated service is stuck in stopping state. The powershell can help you with that too. If you get the process ID that is stuck in starting state, you can just run the second command by substituting the $ServicePID with the process ID. – Rishi Jul 26 '21 at 17:39
  • This worked for me, thanks! I'd love to know why my service is doing this. I'd prefer not to have to do this every time I want to stop a service I'm testing . I feel like there's most likely be a bug in my service and it would be nice to know how to work out what that is! – CodingCretin Aug 11 '21 at 10:31
15

Either your service is busy processing some big operation or is in transition to change the state. hence is not able to accept anymore input...just think of it as taking more than it can chew...

if you are sure that you haven't fed anything big to it, just go to task manager and kill the process for this service or restart your machine.

  • 2
    I just used task manager to kill the service. Then I could restart the service again. thanks. – Fer Oct 04 '18 at 11:16
7

I had exact same problem with Topshelf hosted service. Cause was long service start time, more than 20 seconds. This left service in state where it was unable to process further requests. I was able to reproduce problem only when service was started from command line (net start my_service).

Proper initialization for Topshelf service with long star time is following:

 namespace Example.My.Service
 {
    using System;
    using System.Threading.Tasks;

    using Topshelf;

    internal class Program
    {
        public static void Main()
        {
            HostFactory.Run(
                x =>
                {
                    x.Service<MyService>(
                        s =>
                        {
                            MyService testServerService = null;
                            s.ConstructUsing(name => testServerService = new MyService());
                            s.WhenStarted(service => service.Start());
                            s.WhenStopped(service => service.Stop());
                            s.AfterStartingService(
                                context =>
                                {
                                    if (testServerService == null)
                                    {
                                        throw new InvalidOperationException("Service not created yet.");
                                    }
                                    testServerService.AfterStart(context);
                                });
                        });
                    x.SetServiceName("my_service");
                });
        }
    }

    public sealed class MyService
    {
        private Task starting;

        public void Start()
        {
            this.starting = Task.Run(() => InitializeService());
        }

        private void InitializeService()
        {
            // TODO: Provide service initialization code.
        }

        [CLSCompliant(false)]
        public void AfterStart(HostControl hostStartedContext)
        {
            if (hostStartedContext == null)
            {
                throw new ArgumentNullException(nameof(hostStartedContext));
            }
            if (this.starting == null)
            {
                throw new InvalidOperationException("Service start was not initiated.");
            }
            while (!this.starting.Wait(TimeSpan.FromSeconds(7)))
            {
                hostStartedContext.RequestAdditionalTime(TimeSpan.FromSeconds(10));
            }
        }

        public void Stop()
        {
            // TODO: Provide service shutdown code.
        }
    }
}
blaz
  • 314
  • 4
  • 9
2

I run into a similar issue and found out it was due to one of the services getting stuck in a state of start-pending, stop pending, or stopped. Rebooting the server or trying to restart services did not work. To solve this, I run the Task Manager in the server and in the "Details" tab I located the services that were stuck and killed the process by ending the task. After ending the task I was able to restart services without problem.

In brief: 1. Go to Task Manager 2. Click on "Detail" tab 3. Locate your service 4. Right click on it and stop/kill the process. That is it.

2

I've seen this issue as well, specifically when a service is start pending and I send it a stop programmatically which succeeds but does nothing. Also sometimes I see stop commands to a running service fail with this same exception but then still actually stop the service. I don't think the API can be trusted to do what it says. This error message explanation is quite helpful...

http://technet.microsoft.com/en-us/library/cc962384.aspx

jtruelove
  • 3,306
  • 3
  • 25
  • 29
1

I faced the similar issue. This error sometimes occur because the service can no longer accept control messages, this may be due to disk space issues in the server where that particular service's log file is present. If this occurs, you can consider the below option as well.

  1. Go to the location where the service exe & its log file is located.
  2. Free up some space
  3. Kill the service's process via Task manager
  4. Start the service.
binki
  • 7,754
  • 5
  • 64
  • 110
1

I know it was opened while ago, but i am bit missing the option with Windows command prompt, so only for sake of completeness

  1. Open Task Manager and find respective process and its PID i.e PID = 111
    • Eventually you can narrow down the executive file i.e. Image name = notepad.exe
  2. in command prompt use command TASKKILL
    • example: TASKKILL /F /PID 111 ; TASKKILL /F /IM notepad.exe
FanaS
  • 31
  • 5
1

I had this exact issue internally when starting and stopping a service using PowerShell (Via Octopus Deploy). The root cause for the service not responding to messages appeared to be related to devs accessing files/folders within the root service install directory via an SMB connection (looking at a config file with notepad/explorer).

If the service gets stuck in that situation then the only option is to kill it and sever the connections using computer management. After that, service was able to be redeployed fine.

May not be the exact root cause, but something we now check for.

Jamie
  • 511
  • 7
  • 14
0

I just fought this problem while moving code from an old multi partition box to a newer single partition box. On service stop I was writing to D: and since it didn't exist anymore I got a 1061 error. Any long operation during the OnStop will cause this though unless you spin the call off to another thread with a callback delegate.

TWood
  • 2,563
  • 8
  • 36
  • 58