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?