标题:如何用C#实时获取CPU利用率
只看楼主
shan0yu
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2004-6-12
 问题点数:0 回复次数:3 
如何用C#实时获取CPU利用率

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 %.

[此贴子已经被live41于2004-11-05 23:34:52编辑过]

搜索更多相关主题的帖子: 利用率 CPU 实时 Say void 
2004-06-14 10:19
何凡心
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2004-11-5
得分:0 
不知道说的什么

[此贴子已经被live41于2004-11-05 23:30:23编辑过]


2004-11-05 23:01
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
得分:0 
我也不知道,不过运行了一下,是实时获取CPU运行信息,很不错,给加精了。
2004-11-06 10:38
恋轩念伊人
Rank: 5Rank: 5
来 自:湖南衡阳
等 级:贵宾
威 望:19
帖 子:643
专家分:0
注 册:2008-2-23
得分:0 
想干嘛啊?

我不是一个随便的人,我随便起来不是人我哦。信不?
2008-03-04 15:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-2455-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019613 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved