diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs index 14a2daf46b5edcc60ff2f602277d43ea90d1be32..1655ef07eeee3c83358aeb6c8a48f689133534bd 100644 --- a/ATxService/AutoTx.cs +++ b/ATxService/AutoTx.cs @@ -43,6 +43,9 @@ namespace ATxService private long _txCurFileSize; private int _txCurFileProgress; private int _waitCyclesBeforeNextTx; + public PerformanceCounter CpuCounter; + private float _cpuLoad; + private readonly float[] _cpuLoadLastReadings = {0F, 0F, 0F, 0F}; private DateTime _lastUserDirCheck = DateTime.MinValue; @@ -72,6 +75,7 @@ namespace ATxService private ServiceStatus _status; private static Timer _mainTimer; + private static Timer _loadTimer; #endregion @@ -82,9 +86,16 @@ namespace ATxService /// </summary> public AutoTx() { InitializeComponent(); + SetupFileLogging(); Log.Info("=".PadLeft(80, '=')); Log.Info("Attempting to start {0} service...", ServiceName); + try { + CpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + } + catch (Exception ex) { + Log.Error("initializing PerformanceCounter failed: {0}", ex.Message); + } CreateEventLog(); LoadSettings(); CreateIncomingDirectories(); @@ -365,6 +376,25 @@ namespace ATxService #endregion + private void UpdateCpuLoad(object sender, ElapsedEventArgs elapsedEventArgs) { + _loadTimer.Enabled = false; + try { + Log.Debug("UpdateCpuLoad()"); + Log.Debug(_cpuLoadLastReadings.Average()); + Array.ConstrainedCopy(_cpuLoadLastReadings, 1, _cpuLoadLastReadings, 0, 3); + // var counter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + _cpuLoadLastReadings[3] = CpuCounter.NextValue(); + Log.Debug(_cpuLoadLastReadings.Average()); + Log.Debug("UpdateCpuLoad() finished"); + } + catch (Exception ex) { + Log.Error("UpdateCpuLoad failed: {0}", ex.Message); + } + finally { + _loadTimer.Enabled = true; + } + } + #region overrides for ServiceBase methods (start, stop, ...) /// <summary> @@ -375,6 +405,10 @@ namespace ATxService _mainTimer = new Timer(_config.ServiceTimer); _mainTimer.Elapsed += OnTimedEvent; _mainTimer.Enabled = true; + + _loadTimer = new Timer(1000); + _loadTimer.Elapsed += UpdateCpuLoad; + _loadTimer.Enabled = true; } catch (Exception ex) { Log.Error("Error in OnStart(): {0}", ex.Message);