From 5ee528c4ef50d660c44830d3b5c8e710af0033b3 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Tue, 13 Mar 2018 13:14:59 +0100
Subject: [PATCH] Use a public field to represent the load state

Refers to #36
---
 ATxCommon/Monitoring/Cpu.cs | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/ATxCommon/Monitoring/Cpu.cs b/ATxCommon/Monitoring/Cpu.cs
index 02f5a8b..71aeb85 100644
--- a/ATxCommon/Monitoring/Cpu.cs
+++ b/ATxCommon/Monitoring/Cpu.cs
@@ -55,6 +55,11 @@ namespace ATxCommon.Monitoring
         /// <returns>The average CPU load from the last four readings.</returns>
         public float Load { get; private set; }
 
+        /// <summary>
+        /// Flag representing whether the load is considered to be high or low.
+        /// </summary>
+        public bool HighLoad { get; private set; }
+
         /// <summary>
         /// How often (in ms) to check the CPU load.
         /// </summary>
@@ -116,8 +121,10 @@ namespace ATxCommon.Monitoring
                 Log.Debug("CPU monitoring initializing PerformanceCounter (takes one second)...");
                 _cpuCounter.NextValue();
                 Thread.Sleep(1000);
-                Log.Debug("CPU monitoring current load: {0:0.0}", _cpuCounter.NextValue());
-                // _monitoringTimer = new Timer(_interval);
+                var curLoad = _cpuCounter.NextValue();
+                Log.Debug("CPU monitoring current load: {0:0.0}", curLoad);
+                // now initialize the load state:
+                HighLoad = curLoad > _limit;
                 _monitoringTimer = new Timer(_interval);
                 _monitoringTimer.Elapsed += UpdateCpuLoad;
             }
@@ -177,6 +184,7 @@ namespace ATxCommon.Monitoring
         /// Raise the "LoadAboveLimit" event.
         /// </summary>
         protected virtual void OnLoadAboveLimit() {
+            HighLoad = true;
             LoadAboveLimit?.Invoke(this, EventArgs.Empty);
         }
 
@@ -184,6 +192,7 @@ namespace ATxCommon.Monitoring
         /// Raise the "LoadBelowLimit" event.
         /// </summary>
         protected virtual void OnLoadBelowLimit() {
+            HighLoad = false;
             LoadBelowLimit?.Invoke(this, EventArgs.Empty);
         }
     }
-- 
GitLab