欢迎光临
我们一直在努力

如何用C#实时获取CPU利用率-.NET教程,C#语言

建站超值云服务器,限时71元/月

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)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何用C#实时获取CPU利用率-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章