-2

I am restarting a Windows service successfully on Windows Home Edition, but when I run the app that restarts a Windows service on Windows Server 2019 Datacentre installation, I get this error:

Error 103: The service did not respond to the start or control request in a timely fashion.

public static void EndTask()
{
    string taskname = "serviceTask.exe";
    string processName = taskname.Replace(".exe", "");

    foreach (Process process in Process.GetProcessesByName(processName))
    {
        process.Kill();
        process.WaitForExit();
        process.Dispose();
    }
}
       
msg = string.Empty;
if (serviceName != "")
{
    ServiceController service = new ServiceController(serviceName);
    try
    {
        EndTask();

        int millisec1 = Environment.TickCount;
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
        var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == serviceName);
        if (serviceExists)
        {
            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }

            // count the rest of the timeout
            int millisec2 = Environment.TickCount;
           timeout = TimeSpan.FromMilliseconds(timeoutMillisecond - (millisec2 - millisec1));

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
else
{
    msg = "service not started";

}

The user has admin rights and the service can be started and stopped from the Services panel but not from C#. This is the process I am using to restart. Is there something different that needs to be done on Server 2019?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Karen-S
  • 21
  • 3

1 Answers1

-1

It seems your service has processing time-consuming tasks on the service starting. To increase service time out you can follow this video or you can by-pass your time-consuming tasks on the service starting time by using a timer which will be fired after 5 sec or else, so that service started without taking extra time.

Mahi
  • 1,019
  • 9
  • 19
  • I have tried those and no difference . But how do you bypass the time consuming tasks ? Is there a setting on the service itself ? – Karen-S Nov 16 '20 at 03:47
  • I did not follow any settings. The idea is very straight forward, you need a timer event which will be fired after 5/10 sec, so, when you try to start your service and that time you just initialize the timer, and the service started immediately. After 5/10 sec your configured timer event will be fired and then you can do your time-consuming task. Make sure you disable that timer because that timer used only for bypass time consuming task on the service starting time. – Mahi Nov 16 '20 at 04:25
  • 1
    got it ..now I know what you mean – Karen-S Nov 16 '20 at 05:02