From a4ff897f1d782a685ade56e458fb15f84d2e9fc5 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Sun, 11 Mar 2018 16:05:19 +0100
Subject: [PATCH] Working prototype for monitoring the CPU load in a separate
 thread.

---
 ATxService/AutoTx.cs | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs
index 1655ef0..fb941a8 100644
--- a/ATxService/AutoTx.cs
+++ b/ATxService/AutoTx.cs
@@ -379,13 +379,13 @@ namespace ATxService
         private void UpdateCpuLoad(object sender, ElapsedEventArgs elapsedEventArgs) {
             _loadTimer.Enabled = false;
             try {
-                Log.Debug("UpdateCpuLoad()");
-                Log.Debug(_cpuLoadLastReadings.Average());
+                Log.Trace("UpdateCpuLoad(), old values: {0}, average: {1}",
+                    string.Join(", ", _cpuLoadLastReadings), _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");
+                _cpuLoad = _cpuLoadLastReadings.Average();
+                Log.Trace("UpdateCpuLoad(), new values: {0}, average: {1}",
+                    string.Join(", ", _cpuLoadLastReadings), _cpuLoadLastReadings.Average());
             }
             catch (Exception ex) {
                 Log.Error("UpdateCpuLoad failed: {0}", ex.Message);
@@ -406,7 +406,7 @@ namespace ATxService
                 _mainTimer.Elapsed += OnTimedEvent;
                 _mainTimer.Enabled = true;
 
-                _loadTimer = new Timer(1000);
+                _loadTimer = new Timer(250);
                 _loadTimer.Elapsed += UpdateCpuLoad;
                 _loadTimer.Enabled = true;
             }
@@ -499,6 +499,8 @@ namespace ATxService
             // while this method has not finished yet:
             _mainTimer.Enabled = false;
 
+            Log.Debug("Current CPU load: {0}", _cpuLoad);
+
             try {
                 RunMainTasks();
                 GC.Collect();
-- 
GitLab