c#

位置:IT落伍者 >> c# >> 浏览文章

用C#语言获取CPU利用率


发布日期:2021年04月27日
 
用C#语言获取CPU利用率

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 = %

               

上一篇:C# XML序列化实例浅析

下一篇:C# 语言规范--1.9 接口