forked from lrivera27/acIDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPUMonitor.cs
69 lines (60 loc) · 2.27 KB
/
CPUMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace acIDS
{
//This class will hold all of the CPU monitoring related methods
public class CPUMonitor
{
public static float CPUCounter { get; set; }
public static float LowestCPU { get; set; } = 100;
public static float HighestCPU { get; set; } = 0;
public static float AverageCPU { get; set; }
public static float TotalCPU { get; set; }
public static int Counter { get; set; }
public static string CPUWarnings { get; set; }
public static void StartMonitoring(TextBox usageTextBox, TextBox warningTextBox, int minutes, int seconds)
{
Task.Run(() => { CPUAnomalies.SetNormalUsageByTime(minutes, seconds); });
Task.Run(() => { CPUMonitoring(usageTextBox, warningTextBox); });
}
//Function will read and display current CPU usage
public static async void CPUMonitoring(TextBox usageTextBox, TextBox warningTextBox)
{
PerformanceCounter cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
while (true)
{
//First value always returns a 0
var unused = cpuCounter.NextValue();
await Task.Delay(1000);
usageTextBox.Invoke(new Action(() =>
{
CPUCounter = cpuCounter.NextValue();
usageTextBox.Text = CPUCounter.ToString("F2") + "%";
}));
CPUCalculations();
CPUAnomalies.StartAnomalyDetection(warningTextBox);
if (mainMenu.done)
break;
}
}
public static void CPUCalculations()
{
if (HighestCPU < CPUCounter)
HighestCPU = CPUCounter;
else if (LowestCPU > CPUCounter)
LowestCPU = CPUCounter;
Counter++;
TotalCPU += CPUCounter;
AverageCPU = TotalCPU / Counter;
}
}
}