Can a custom windows service stop/start/restart itself? Created in Visual studio 2019 c#

Darryl Hoar 181 Reputation points
2024-12-03T21:23:13.1+00:00

Created a custom windows service in c# using visual studio 2019.

I have created a custom winform application that communicates with the custom service using sockets. Since users don't have admin rights, its a pain to stop/start/restart the service.

Wondering if I could send a command over the socket to the custom service and have it stop/start/restart itself.

thanks for any help

Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. RLWA32 49,551 Reputation points
    2024-12-03T22:19:34.58+00:00

    If you really want non-administrator accounts to be able to start/stop/restart your service you can accomplish that by adjusting the security descriptor of the service. It would probably be best to have this done by the installer of the service but it could also be accomplished after installation by an Administrator using Windows sc.exe command. For example, take a look at SO's Start / Stop a Windows Service from a non-Administrator user account. I haven't tested the specifics but it conveys the idea.


  2. Michael Taylor 60,161 Reputation points
    2024-12-03T22:23:18.1933333+00:00

    A service can stop itself of course. A stopped service cannot restart itself since there is nothing to run.

    There are a couple of workarounds for this. The easiest workaround is to have the service accept a custom command (this is built into SCM and doesn't need any sort of custom listener on the services side, unless you already have one). When the service receives the command it does one of the following.

    For stop, just have the service call it's Stop method.

    For restart have the service spawn a new process that stops the service and then starts the service back up. The process itself can be a simple script file (batch or powershell or whatever) that calls the SCM to communicate with the service. You can generate the script file dynamically with any arguments you might need and then run it. The process will run in the context of the service account. Assuming the service account runs with sufficient privileges then the process can restart the service. If the service isn't running with sufficient privileges then it cannot restart itself.

    The new process may need a delay to prevent it from stopping the service before the service has had time to finish executing the command that triggered it but that shouldn't take too long. Alternatively the service could stop itself after starting the new process. The new process would then need to poll the service until it has stopped before it tries to start it again.

    An alternative approach is to create a scheduled task that periodically checks to see if the service is running. If it isn't then restart it. The plus side to this is that it is easy to implement. The downside is that it needs to balance running too often with how long you're OK with the service being offline. Also when you want to actually stop the service (perhaps for updates or whatever) then the scheduled task would also need to be disabled otherwise it would restart it on you. This is the approach that many malware prevention tools use to ensure users don't stop the tool from running.

    If the service is stopped then it cannot restart itself as there is no place to run the code. For this scenario you'd need a standalone app that runs constantly that has sufficient privileges. This is what SQL Server used to do. However if you're going to do that then you might as well let your main app support stopping/starting the service.

    Finally note that services that fail can be configured to auto-restart as well. So if you just need to ensure your service is always running then that can be configured as part of service creation.

    0 comments No comments

  3. Bruce (SqlWork.com) 77,851 Reputation points Volunteer Moderator
    2024-12-03T23:33:28.14+00:00

    if the service installer configure security such that the user can start/stop the service, then your application running under the users context can start and stop the service. the easiest method is run net start command line, but you can use the api:https://learn.microsoft.com/en-us/windows/win32/services/starting-a-service

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.