Skip to content
Snippets Groups Projects
Commit bb8a8505 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Prototype for using PerformanceCounter to get CPU load.

parent fac36689
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment