当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 用C#语言获取CPU利用率

ASP.NET
基于C#的接口基础教程之五
基于C#的接口基础教程之六
ASP.NET中数据库的操作初步----增加、删除、修改
从Internet上抓取指定URL的源码的方案(C#)
对C#中正则表达式的一些解读和总结
简述c#中对字符串进行分割的几种方法
ASP.NET中的事务处理和异常处理
用ASP.NET/C#连接Access和SQL Server数据库
ASP.NET 2.0中层次数据的处理
ASP.NET 2.0服务器控件之客户端功能
asp.net2.0 URL重写以及urlMappings问题(1)
asp.net2.0 URL重写以及urlMappings问题(2)
再议ASP.NET DataGrid控件中的“添加新行”功能
建立个人知识引擎的重要性
解析.Net框架下的XML编程技术
ASP.NET--制作功能完善的安装程序
Asp.net直接保存文件到客户端
.Net中消除Dll中的dependency
ASP.NET页面事件:顺序与回传详解
在ASP.NET+ORACLE添加数据记录并让ID自动增量

ASP.NET 中的 用C#语言获取CPU利用率


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 82 ::
收藏到网摘: n/a

using System;
using System.Diagnostics;
using System.Threading;


public class CpuLoadInfo
{

// auxiliary print methods
private static void Say ( string txt )
{
Console.WriteLine(txt);
}

// auxiliary print methods
private static void Say()
{
Say("");
}

// The main method. Command line arguments are ignored.
[STAThread]
public static void Main()
{
Say("$Id: CpuLoadInfo.cs,v 1.2 2002/08/17 17:45:48 rz65 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(CategoryName,CounterName,InstanceName);
Say("Performance counter was created.");
Say("Property CounterType: " + pc.CounterType);
Say();

Say("Property CounterHelp: " + pc.CounterHelp);
Say();
Say("Entering measurement loop.");

while (true)
{
Thread.Sleep(1000); // wait for 1 second
float cpuLoad = pc.Nextvalue();
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 = 0 %.
CPU load = 1.941746 %.
CPU load = 4.854369 %.
CPU load = 10 %.
CPU load = 0 %.
CPU load = 2.999997 %.
CPU load = 0.9900987 %.
CPU load = 0 %.