#define TRACE using System; using System.Diagnostics; using System.Globalization; using System.ServiceProcess; using System.Timers; namespace ArchestrAServices.Common; public class WindowsServiceWatcher : IDisposable { private static readonly int TimerInterval = 5000; private static readonly int LogWarningCount = 12; private readonly string serviceName; private bool disposed; private Timer timer; private int isOfflineCounter; private bool lastIsRunningStatus; private ServiceController serviceController; public event EventHandler StatusChanged; public WindowsServiceWatcher(string serviceName) { this.serviceName = serviceName; timer = new Timer(TimerInterval); timer.Elapsed += MonitorServiceStatus; timer.Enabled = false; timer.AutoReset = false; } public void Start() { MonitorServiceStatus(null, null); timer.Enabled = true; } public void Stop() { timer.Enabled = false; } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { if (timer != null) { timer.Elapsed -= MonitorServiceStatus; timer.Dispose(); timer = null; } if (serviceController != null) { serviceController.Dispose(); serviceController = null; } lastIsRunningStatus = false; isOfflineCounter = 0; } disposed = true; } private void MonitorServiceStatus(object source, ElapsedEventArgs e) { bool flag = false; bool flag2; try { if (serviceController == null) { serviceController = new ServiceController(serviceName); } serviceController.Refresh(); flag2 = serviceController.Status == ServiceControllerStatus.Running; } catch (Exception) { if (serviceController != null) { serviceController.Dispose(); serviceController = null; } flag2 = false; flag = true; } if (flag2) { if (!lastIsRunningStatus) { SvcTrace.DiagControl.TraceEvent(TraceEventType.Information, 0, string.Format(CultureInfo.InvariantCulture, "{0} windows service is running.", new object[1] { serviceName })); OnStatusChanged(new WindowsServiceStatusChangedEventArgs { IsRunning = true }); lastIsRunningStatus = true; isOfflineCounter = 0; } } else { if (lastIsRunningStatus) { OnStatusChanged(new WindowsServiceStatusChangedEventArgs { IsRunning = false }); lastIsRunningStatus = false; } isOfflineCounter++; if (isOfflineCounter % LogWarningCount == 0) { if (flag) { SvcTrace.DiagControl.TraceEvent(TraceEventType.Error, 0, string.Format(CultureInfo.InvariantCulture, "{0} windows service does not exist. If this problem persists, please try installing the {0} windows service.", new object[1] { serviceName })); } else { SvcTrace.DiagControl.TraceEvent(TraceEventType.Warning, 0, string.Format(CultureInfo.InvariantCulture, "{0} windows service is not running. If this problem persists, please try restarting the {0} windows service.", new object[1] { serviceName })); } } } timer.Enabled = true; } private void OnStatusChanged(WindowsServiceStatusChangedEventArgs newStatus) { if (this.StatusChanged != null) { this.StatusChanged(this, newStatus); } } }