ServiceController serviceController = null;
try
{
serviceController = new("name");
serviceController.Refresh();
if (serviceController.Status == ServiceControllerStatus.Stopped)
{
if (serviceController.StartType == ServiceStartMode.Disabled)
{
}
serviceController.Start();
serviceController.Refresh();
serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
serviceController.Refresh();
var s = serviceController.Status;
}
}
catch (Exception ex)
{
}
I am trying to start my service if found the service is stopped. What I manually to make the service to stop is stop the service and delete a critical file, so then the service cannot be started. And then code will be go into the if statement. but after I did start() and WaitForStatus(ServiceControllerStatus.Running), it got running. and if I change the call to WaitForStatus(ServiceControllerStatus.Stopped), it got stopped. that is weird. I am using .net 6.