分享

实时获取CPU使用率

 天涯1990 2010-11-12
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.
 
 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";
}
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多