当前位置:文章中心 >> vs2022_vs2019_vs2017_vs2014_vs2012
|
用C#语言获取CPU利用率
|
夜鹰教程网 来源:www.yyjcw.com 日期:2016-11-25 21:43:39
|
这篇文章不能解决你的问题?我们还有相关视频教程云课堂 全套前端开发工程师培训课程 微信号:yyjcw10000 QQ:1416759661 远程协助需要加QQ! 业务范围:视频教程|程序开发|在线解答|Demo制作|远程调试| 点击查看相关的视频教程。 技术范围:全端开发/前端开发/webapp/web服务/接口开发/单片机/C#/java/node/sql server/mysql/mongodb/android/。
|
using System; using System.Diagnostics; using System.Threading; 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 %.
|
|
热门服务/教程目录
|
客服电话:153 9760 0032
购买教程QQ:1416759661
|
|
|