using System;
using SystemDiagnostics;
using SystemThreading;
public class CpuLoadInfo
{
// auxiliary print methods
private static void Say ( string txt )
{
ConsoleWriteLine(txt);
}
// auxiliary print methods
private static void Say()
{
Say();
}
// The main method Command line arguments are ignored
[STAThread]
public static void Main()
{
Say($Id: CpuLoadInfocsv // :: rz Exp $);
Say();
Say(Attempt to create a PerformanceCounter instance:);
Say(Category name = + CategoryName);
Say(Counter name = + CounterName);
Say(Instance name = + InstanceName);
PerformanceCounter pc
= new PerformanceCounter(CategoryNameCounterNameInstanceName);
Say(Performance counter was created);
Say(Property CounterType: + pcCounterType);
Say();
Say(Property CounterHelp: + pcCounterHelp);
Say();
Say(Entering measurement loop);
while (true)
{
ThreadSleep(); // wait for second
float cpuLoad = pcNextvalue();
Say(CPU load = + cpuLoad + %);
}
}
// constants used to select the performance counter
private const string CategoryName = Processor;
private const string CounterName = % Processor Time;
private const string InstanceName = _Total;
}
这是在我计算机上的计算结果
Entering measurement loop
CPU load = %
CPU load = %
CPU load = %
CPU load = %
CPU load = %
CPU load = %
CPU load = %
CPU load = %