private static void TimerEventProcessor(Object sender, System.Timers.ElapsedEventArgs e) { string serviceName = "Spooler"; ServiceController service = new ServiceController(serviceName); ServiceControllerStatus status = service.Status; if (!status.Equals(ServiceControllerStatus.Running)) { try { string msg = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"); msg += " Spooler service had stoped"; System.IO.StreamWriter file = new System.IO.StreamWriter(folderPath, true); file.WriteLine(msg); file.Close(); System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(@"C:\Windows\System32\spool\PRINTERS\"); foreach (FileInfo prfile in downloadedMessageInfo.GetFiles()) { prfile.Delete(); } service.Start(); } catch { } } }
/// <summary> /// Returns true if JHI status equals to the expected status and false otherwise /// </summary> public static bool CheckJhiStatus(ServiceControllerStatus expectedStatus) { ServiceControllerStatus jhiStatus = GetJhiStatus(); if (!expectedStatus.Equals(jhiStatus)) { return(false); } return(true); }
public void UpdateStatus() { bool service = false; var converter = new System.Windows.Media.BrushConverter(); try { ServiceController sc = new ServiceController("FastIR"); ServiceControllerStatus scs = sc.Status; if (scs.Equals(ServiceControllerStatus.Running)) { Label_FastIR_Service.Foreground = (Brush)converter.ConvertFromString("#008000"); Label_FastIR_Service.Text = "RUNNING"; service = true; StopSC.IsEnabled = true; StartSC.IsEnabled = false; } else { Label_FastIR_Service.Foreground = (Brush)converter.ConvertFromString("#FF0000"); Label_FastIR_Service.Text = "STOPPED"; service = false; StopSC.IsEnabled = false; StartSC.IsEnabled = true; } sc.Close(); } catch { Label_FastIR_Service.Foreground = (Brush)converter.ConvertFromString("#008B8B"); Label_FastIR_Service.Text = "does not exist"; service = false; StopSC.IsEnabled = false; StartSC.IsEnabled = false; } if (service.Equals(true)) { if (d.CheckURL()) { Label_FastIR_Network.Foreground = (Brush)converter.ConvertFromString("#008000"); Label_FastIR_Network.Text = "OK"; } else { Label_FastIR_Network.Foreground = (Brush)converter.ConvertFromString("#FF0000"); Label_FastIR_Network.Text = "KO"; } Final.Text = "FastIR Agent is working on the system."; } else { Label_FastIR_Network.Foreground = (Brush)converter.ConvertFromString("#008B8B"); Label_FastIR_Network.Text = "N/A"; Final.Text = "FastIR Agent does work on the system..."; } }
private void setServiceStatus(ServiceControllerStatus status) { if (serviceController.Status.Equals(status)) //already in the wanted status { OnProgress(processIdx + 1); } else { try { TimeSpan timeoutSec = TimeSpan.FromSeconds(timeout); bool tryAgain = false; int tryCount = 0; do { try { if (status.Equals(ServiceControllerStatus.Running)) serviceController.Start(); //TODO: support arguments else serviceController.Stop(); } catch (InvalidOperationException e) { //in some cases, the service status might be changed although the exception is thrown Thread.Sleep(TimeSpan.FromSeconds(5)); //wait a little bit time to try again serviceController.Refresh(); if (serviceController.Status.Equals(status)) //equal tryAgain = false; else if (tryCount < TOTAL_TRY) //not equal, not reach total tryAgain = true; else //when not equal to status, AND reach total try throw e; tryCount++; } } while (tryAgain); serviceController.WaitForStatus(status, timeoutSec); Console.WriteLine(serviceController.Status.ToString()); //check the status to make sure it is started properly if (serviceController.Status.Equals(status)) { OnProgress(processIdx + 1); } else { throw new InvalidOperationException(serviceController.ServiceName + " cannot be changed to status " + status.ToString()); } } //TODO: there should be configuration for the app //when error occurs to a service, should it stop or continue to next service? catch (InvalidOperationException e) { OnError(e); } catch (System.ServiceProcess.TimeoutException e) { OnError(e); } } }