private Message ServiceRestart(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { int millisec1 = Environment.TickCount; TimeSpan 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); Message msg = new Message(); msg.title = "Service Restarted"; msg.message = "Service '" + serviceName + "' has been restarted."; msg.messageType = Message.MessageType.Information; return msg; } catch(System.ServiceProcess.TimeoutException ex) { Message msg = new Message(); msg.title = "Service Error - Time Out"; msg.message = "Service '" + serviceName + "' could not be restarted." + System.Environment.NewLine + ex.ToString(); msg.messageType = Message.MessageType.Error; return msg; } catch(Exception ex) { Message msg = new Message(); msg.title = "Unknown Service Error"; msg.message = "Service '" + serviceName + "' could not be restarted." + System.Environment.NewLine + ex.ToString(); msg.messageType = Message.MessageType.Error; return msg; } }
private Message ServiceAction(string serviceName, int timeOut, ServiceActions action ) { Message msg = new Message(); ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeOut); switch (action) { case ServiceActions.Start: service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); break; case ServiceActions.Stop: service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); break; default: break; } } catch (System.ServiceProcess.TimeoutException ex) { // Service timed out trying to restart msg.title = "Service Time Out"; msg.message = ex.Message; msg.messageType = Message.MessageType.Error; } catch (Exception ex) { msg.title = "Unknown Error"; msg.message = "Unknown error attempting to " + action.ToString().ToLower() +" service '" + serviceName + "'." + System.Environment.NewLine + ex.Message; } return msg; }
public Message StopService(string serviceName, int timeOut) { Message msg = new Message(); msg = ServiceAction(serviceName, timeOut, ServiceActions.Stop); return msg; }
public Message StopService(string serviceName) { Message msg = new Message(); msg = ServiceAction(serviceName, 10000, ServiceActions.Stop); return msg; }
/// <summary> /// Start a Windows service /// </summary> /// <param name="serviceName">The name of the service to start</param> /// <param name="timeOut">How long to wait (in milliseconds) before timing out</param> /// <returns></returns> public Message StartService(string serviceName, int timeOut) { Message message = new Message(); message = ServiceAction(serviceName, timeOut, ServiceActions.Start); return message; }
/// <summary> /// Start a Windows Service /// </summary> /// <param name="serviceName">The name of the service to start</param> /// <returns></returns> public Message StartService(string serviceName) { Message message = new Message(); message = ServiceAction(serviceName, 10000, ServiceActions.Start); return message; }