/// <summary> /// Restarts NPrinting's web engine service asynchronously while raising status /// updates for event. /// </summary> /// <returns>True if successful, and false if not.</returns> public async Task <bool> RestartWebEngineServiceAsync() { var service = new ServiceController("Qlik NPrinting Web Engine"); TimeSpan timeout = TimeSpan.FromMinutes(1); var args = new ServiceStatusChangedEventArgs(); try { return(await Task.Run(() => { if (service.Status != ServiceControllerStatus.Stopped) { service.Stop(); args.Status = "Stopping NPrinting web engine service..."; OnServiceStatusChanged(args); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } service.Start(); args.Status = "Starting NPrinting web engine service..."; OnServiceStatusChanged(args); service.WaitForStatus(ServiceControllerStatus.Running, timeout); args.Status = "Finished restarting service."; OnServiceStatusChanged(args); return true; })); } catch (InvalidOperationException) { args.Status = "Failed to restart service."; args.ErrorMessage = "The service was not found on this computer."; OnServiceStatusChanged(args); } catch (TimeoutException) { args.Status = "Failed to restart service."; args.ErrorMessage = "The service timed out."; OnServiceStatusChanged(args); } return(false); }
/// <summary> /// Event to raise for status changes when restarting service. /// </summary> /// <param name="e">Status details to pass with event-</param> protected virtual void OnServiceStatusChanged(ServiceStatusChangedEventArgs e) { EventHandler <ServiceStatusChangedEventArgs> handler = ServiceStatusChanged; handler?.Invoke(this, e); }