//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 StartMonitoring(TextBox usageTextBox, TextBox warningTextBox, int minutes, int seconds) { Task.Run(() => { CPUAnomalies.SetNormalUsageByTime(minutes, seconds); }); Task.Run(() => { CPUMonitoring(usageTextBox, warningTextBox); }); }