请帮忙看看,CPU站用率代码错在那里?

cainiao000 2005-01-06 03:49:29
string CategoryName = "Processor";
string CounterName = "% Processor Time";
string InstanceName = "_Total";


PerformanceCounter pc= new PerformanceCounter(CategoryName,CounterName,InstanceName);

float cpuLoad = pc.NextValue();
label2.Text="CPU:"+ cpuLoad + " %";
----------------------------------------------------------
在我计算机上不断刷新还是'CPU:0%',而企业管理器中却有非零数值.

我是在这里参考的.
http://www.programfan.com/showarticle.asp?id=2249
...全文
147 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lucbesson 2005-01-06
  • 打赏
  • 举报
回复
你写在winform下还是win32控制台应用程序下啊 ?

你也没贴出你写在什么地方,如果在winform下要做一下改动的,如果不是,没什么错啊,
我调试了一下。
winfrom下 使用timer来替换sleep方法 !
nga96 2005-01-06
  • 打赏
  • 举报
回复
这个代码编译能通过么
hanbinghai 2005-01-06
  • 打赏
  • 举报
回复
^_^,开始没注意,性能计数器名称里有"% Processor Time"
hanbinghai 2005-01-06
  • 打赏
  • 举报
回复
private const string CounterName = "% Processor Time";?
我怎么没发现有呢?

using System.Diagnostics;
using System.Threading;
......
private void Form1_Load(object sender, System.EventArgs e)
{
PerformanceCounterCategory[] myCat2;
myCat2 = PerformanceCounterCategory.GetCategories();
for (int i=0;i<myCat2.Length;i++)
{
if("Processor" == myCat2[i].CategoryName)
{
Debug.WriteLine("CategoryName: ");
Debug.Write(myCat2[i].CategoryName);

Debug.WriteLine("CategoryName: ");
string[] str =myCat2[i].GetInstanceNames();
for(int k=0;k< str.Length;k++)
{
Debug.WriteLine(k.ToString() + " " + str[k]);
PerformanceCounter[] per = myCat2[i].GetCounters(str[k]);
Debug.WriteLine("PerformanceCounter: ");
for(int j=0;j<per.Length;j++)
{
Debug.WriteLine(" (" + j.ToString() + ") " +per[j].CounterName);
}
}

}
}
}
我输出的结果是:
CategoryName:
ProcessorCategoryName:
0 _Total
PerformanceCounter:
(0) % Processor Time
(1) % User Time
(2) % Privileged Time
(3) Interrupts/sec
(4) % DPC Time
(5) % Interrupt Time
(6) DPCs Queued/sec
(7) DPC Rate
(8) % Idle Time
(9) % C1 Time
(10) % C2 Time
(11) % C3 Time
(12) C1 Transitions/sec
(13) C2 Transitions/sec
(14) C3 Transitions/sec
1 0
PerformanceCounter:
(0) % Processor Time
(1) % User Time
(2) % Privileged Time
(3) Interrupts/sec
(4) % DPC Time
(5) % Interrupt Time
(6) DPCs Queued/sec
(7) DPC Rate
(8) % Idle Time
(9) % C1 Time
(10) % C2 Time
(11) % C3 Time
(12) C1 Transitions/sec
(13) C2 Transitions/sec
(14) C3 Transitions/sec
stephenZL 2005-01-06
  • 打赏
  • 举报
回复
PerformanceCounter pc= new PerformanceCounter(CategoryName,CounterName,InstanceName);
不能放在按钮事件,放在Form1_Load里面!
liulxmooo 2005-01-06
  • 打赏
  • 举报
回复
up
cainiao000 2005-01-06
  • 打赏
  • 举报
回复
PerformanceCounter pc= new PerformanceCounter(CategoryName,CounterName,InstanceName);

float cpuLoad = pc.NextValue();
label2.Text="CPU:"+ cpuLoad + " %";


这部分代码我放在一个按钮事件下面,不停点按钮还是'CPU:0%'.
stephenZL 2005-01-06
  • 打赏
  • 举报
回复
float cpuLoad = pc.NextValue();
label2.Text="CPU:"+ cpuLoad + " %";
代码放在Timer1_tick事件里面,然后将Timer1的Enable设为true,interval设为1000,表示每一秒钟对CPU占用率做一次采样,然后打出来
cainiao000 2005-01-06
  • 打赏
  • 举报
回复
是的.
flyskywlh 2005-01-06
  • 打赏
  • 举报
回复
你是将它的代码原样复制过来的吗?
cainiao000 2005-01-06
  • 打赏
  • 举报
回复
更正:
在我计算机上不断刷新还是'CPU:0%',而任务管理器中却有非零数值.

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧