ServiceController seems to be unable to stop a service

Anton Gogolev picture Anton Gogolev · Jun 9, 2011 · Viewed 89k times · Source

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?

Answer

Rishi picture Rishi · Aug 17, 2016

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