//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 void GetCPU() // TODO, calculate this based on affinities { if (Process != null) { try { var currentprocname = GetProcessName(); if (CPUCounter == null || (CPUCounter.InstanceName != currentprocname)) { CPUCounter = new PerformanceCounter("Process", "% Processor Time", currentprocname); } DataRow.Cells[4].Value = Math.Round(CPUCounter.NextValue(), 1) + "%"; } catch (Exception) { } } }
/// <summary> /// Stops the timer and registers the information /// </summary> public void Stop() { if (Current == null) { if (CPUCounter != null) { CPUCounter.Dispose(); CPUCounter = null; } return; } if (Current.Running) { Current.Running = false; Current.StopWatch.Stop(); Current.Entries.Add(new Entry(Current.StopWatch.ElapsedTime, MemValue, CPUValue)); Current = Parent; } }