2024年3月5日发(作者:)
利用C#监控计算机CPU的使用率
利用C#监控计算机CPU的使用率
今天在网上看到一段比较好玩的代码,用来显示本地计算机CPU的使用情况,整理如下,欢迎使用,更多信息请浏览店铺!
using System;
using stics;
using ing;
namespace ConsoleApplication1
{
class Program
{
private const string CategoryName = "Processor";
private const string CounterName = "% Processor Time";
private const string InstanceName = "_Total";
private static void OutPut(string txt)
{
ine(txt);
}
[STAThread]
public static void Main()
{
PerformanceCounter
InstanceName);
OutPut("----------------------------开始创造性能计数器----------------------------");
OutPut("计数器类型:" + rType);
OutPut("计数器的说明:" + rHelp);
OutPut("开始输出:");
pc = new
PerformanceCounter(CategoryName, CounterName,
while (true)
{
(1000);
float cpuLoad = lue();
OutPut("CPU 耗用:" + cpuLoad + "%");
}
}
}
}
直接运行程序,对比控制台输出的结果与计算机的Windows任务管理器中显示的CPU使用值,会发现原来Windows任务管理器显示的值是CPU使用率的'四舍五入的结果。有兴趣的朋友可以自己复制代码试试看啦。
发布评论