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
|
using System;
using System.Diagnostics;
static PerformanceCounter cpuCounter; // globaler PerformanceCounter
static void InitialisierePerformanceCounter() // Initialisieren
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total"; // "_Total" entspricht der gesamten CPU Auslastung, Bei Computern mit mehr als 1 logischem Prozessor: "0" dem ersten Core, "1" dem zweiten...
}
static float GetCPUusage() // Liefert die aktuelle Auslastung zurück
{
return cpuCounter.NextValue();
}
/*
Beispiel zur Verwendung
*/
static void Main(string[] args)
{
InitialisierePerformanceCounter(); // Initialisieren
while (true)
{
Console.WriteLine(GetCPUusage()); // CPU Auslastung in die Konsole schreiben
System.Threading.Thread.Sleep(1000); // 1 Sekunde warten
}
}
|