Start Windows Service programmatically

Solution 1:

If the code you showed is executing on a different machine than where the service is supposed to run (I'm not clear from your comments if that's the case or not), you would need to provide the machine name in the ServiceController constructer.

Is it possible you are successfully starting the service, but not on the machine you think? That would fit the symptoms you describe.

ServiceController service = new ServiceController(serviceName, serverName);

Also see ServiceController constructor documentation.

Solution 2:

Here is code I have in a Window Service responsible for stopping starting other services running on the same server.

ServiceController controller = new ServiceController(serviceName);
if (controller.Status==ServiceControllerStatus.Running)
    controller.Stop();

if (controller.Status==ServiceControllerStatus.Stopped)
    controller.Start(); 

Solution 3:

This is an old thread, but google got me here. My services, even after successfully starting, would still query back as stopped. If you add

service.Refresh();

It will query the actual current status instead of the stored state from a previous query. I don't know why it works like this, but it does.

P.S.: I added a minute long time out and still got a service is stopped response without the refresh.

Solution 4:

public static void StartService(string serviceName, int timeoutMilliseconds)
    {
        ServiceController service = new ServiceController(serviceName);
        try
        {
            int millisec1 = 0;
            TimeSpan timeout;

            // count the rest of the timeout
            int millisec2 = Environment.TickCount;
            timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec1));
            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);

        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message);
        }
    }
    public static void StopService(string serviceName, int timeoutMilliseconds)
    {
        ServiceController service = new ServiceController(serviceName);
        try
        {
            int millisec1 = 0;
            TimeSpan timeout;
            if (service.Status == ServiceControllerStatus.Running)
            {
                millisec1 = Environment.TickCount;
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }


        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message);
        }
    }
    public static void RestartService(string serviceName, int timeoutMilliseconds)
    {
        ServiceController service = new ServiceController(serviceName);
        try
        {
            int millisec1 = 0;
            TimeSpan timeout;
            if (service.Status == ServiceControllerStatus.Running)
            {
                millisec1 = Environment.TickCount;
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }
            // count the rest of the timeout
            int millisec2 = Environment.TickCount;
            timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);

        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message);
        }
    }

Dont forget to add ServiceProcess as referance using System.ServiceProcess;

Solution 5:

First of all you need to add reference of the DLL (ServiceProcess) in your project References as like:

Right click on References in Solution Explorer -> Add Reference -> Assemblies -> 
Framework -> System.ServiceProcess

Then add ServiceProcess DLL in your project:

using System.ServiceProcess;

After that use this code:

ServiceController service = new ServiceController(yourServiceName);