public static void StopService(string serviceName, int timeoutSeconds) { using (ServiceController controller = new ServiceController(serviceName)) { try { controller.Stop(); controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(timeoutSeconds)); } catch (InvalidOperationException ex) { // Service may already be stopped } } }