當(dāng)前位置:首頁>>開發(fā)編程>>VS.NET>>新聞內(nèi)容
如何用C#實時獲取CPU利用率
作者:孟憲會 發(fā)布時間:2004-8-24 9:55:10 文章來源:孟憲會之精彩世界
<!--StartFragment-->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";
}

這是在我計算機(jī)上的計算結(jié)果:
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 %.

最新更新
·C#中使用Split分隔字符串的技
·VS2008開發(fā)中Windows Mobile
·PC機(jī)和移動設(shè)備上絕對路徑的
·C#程序加殼的方法(使用Sixx
·當(dāng)前上下文中不存在名稱Conf
·請插入磁盤:Visual Studio 2
·用VS.NET讀取Flash格式文件信
·在ASP.NET中使用AJAX的簡單方
·VS.NET 2005中常用的一些代碼
·安裝VS.NET 2005 SP1補(bǔ)丁全攻
相關(guān)信息
·C#中使用Split分隔字符串的技巧
·PC機(jī)和移動設(shè)備上絕對路徑的獲取(C#)
·C#程序加殼的方法(使用Sixxpack)
·當(dāng)前上下文中不存在名稱ConfigurationManager的解決方法
·C#的支付寶Payto接口代碼
·C#實現(xiàn)窗口最小化到系統(tǒng)托盤
·解密QQ的MsgEx.db消息文件格式
·QQ的TEA填充算法C#實現(xiàn)
·C#用Guid獲取不規(guī)則的唯一值(標(biāo)識)
·基于Windows Mobile 5.0的掌上天氣預(yù)報設(shè)計
畫心
愚愛
偏愛
火苗
白狐
畫沙
犯錯
歌曲
傳奇
稻香
小酒窩
獅子座
小情歌
全是愛
棉花糖
海豚音
我相信
甩蔥歌
這叫愛
shero
走天涯
琉璃月
Nobody
我愛他
套馬桿
愛是你我
最后一次
少女時代
灰色頭像
斷橋殘雪
美了美了
狼的誘惑
我很快樂
星月神話
心痛2009
愛丫愛丫
半城煙沙
旗開得勝
郎的誘惑
愛情買賣
2010等你來
我叫小沈陽
i miss you
姑娘我愛你
我們都一樣
其實很寂寞
我愛雨夜花
變心的玫瑰
犀利哥之歌
你是我的眼
你是我的OK繃
貝多芬的悲傷
哥只是個傳說
丟了幸福的豬
找個人來愛我
要嫁就嫁灰太狼
如果這就是愛情
我們沒有在一起
寂寞在唱什么歌
斯琴高麗的傷心
別在我離開之前離開
不是因為寂寞才想你
愛上你等于愛上了錯
在心里從此永遠(yuǎn)有個你
一個人的寂寞兩個人的錯