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

ASP.NET
从ASP.NET的PHP执行速度比较谈起
基于ASP.NET2.0的非HttpModule山寨版MVC框架的实现
ASP.NET 中公有的六种验证控件
无废话C#设计模式之八:Facade
C# 中的 @ 符号的使用及注意事项
保护你的.Net代码:谁动了我的组件?
VS2008与.NET 3.5的JS智能感知和调试
关于 .Net 开发下的分布式缓存设计
在C#3.0中使用扩展方法来扩展接口
C#实现Windows 服务的制作安装和删除
C#3.0中实现隐式类型变量、匿名类型的方法
C#代码与javaScript函数的相互调用
构建 ASP.NET AJAX 开发环境
Windows 窗体的.Net 框架绘图技术
.NET 开发中的一些小技巧集锦
ASP.NET 2.0 创建母版页引来的麻烦
.NET Compact Framework 概述
.NET Compact Framework 2.0 SP1新增功能
专家访谈:从Web 2.0到Web 3.0
.Net里漂浮窗口拖动的实现方法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 62 ::
收藏到网摘: 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 %.