当前位置:首页 > 网络编程 > 软件语言 > .NET > 如何用C#实时获取CPU利用率

如何用C#实时获取CPU利用率

点击次数:85 次 发布日期:2008-11-06 08:08:04 作者:源代码网
源代码网推荐
广告载入中
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 %.
源代码网推荐

源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华