编写程序 控制cpu占用率-3
2011-10-12 19:22
274 查看
上一节讲了通过GetTickCount()控制时间片的切换,然而MS .NET FRAMEWORK还提供了PerformanceCounter这一对象,可以获得系统资源的各种性能数据,通过这个PerformanceCounter 对象,我们可以更准确的获得CPU的信息。
上一节连接:
http://blog.csdn.net/weixingstudio/article/details/6867373
在 Console.WriteLine("*******************************************************"); 语句前的操作暂时不用管,因为我是测试用的。
通过设定一个level值,当CPU的使用率超过这个值以后让其休眠,否则进行不停的空循环。
但是结果并不是理想,因为同样像上一节那样出现了一个内核繁忙一个内核空闲的现象,稍后将解决双核处理器的问题。
上一节连接:
http://blog.csdn.net/weixingstudio/article/details/6867373
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace cpu_3_csharp { class Program { static void Main(string[] args) { float level=150F; PerformanceCounterCategory[] test = PerformanceCounterCategory.GetCategories(); for (int i = 0; i < test.Length; i++) { //Console.WriteLine(test[i].CategoryName); if (test[i].CategoryName == "Processor") { string[] temp = test[i].GetInstanceNames(); //Console.WriteLine(temp.Length); for (int j = 0; j < temp.Length; j++) { Console.WriteLine(test[i].MachineName); Console.WriteLine("------------------------"); Console.WriteLine(temp[j]); Console.WriteLine("------------------------"); PerformanceCounter[] counters = test[i].GetCounters(temp[j]); for (int k = 0; k < counters.Length; k++) { Console.WriteLine(counters[k].CounterName); } } } } Console.WriteLine("*******************************************************"); PerformanceCounter p = new PerformanceCounter("Processor","% Processor Time","_Total"); Console.WriteLine(p.NextValue()); while (true) { //Console.WriteLine(p.NextValue()); if (p.NextValue() > level) { System.Threading.Thread.Sleep(10); } } } } }
在 Console.WriteLine("*******************************************************"); 语句前的操作暂时不用管,因为我是测试用的。
通过设定一个level值,当CPU的使用率超过这个值以后让其休眠,否则进行不停的空循环。
但是结果并不是理想,因为同样像上一节那样出现了一个内核繁忙一个内核空闲的现象,稍后将解决双核处理器的问题。
相关文章推荐
- 编写程序 控制cpu占用率2
- 编写程序 控制cpu占用率
- 编写程序 控制cpu占用率-4 绘制CPU使用率的正弦曲线
- 写程序+控制cpu占用率-4+绘制CPU使用率的正弦曲线2+C#+实现
- 写程序 控制cpu占用率-4 绘制CPU使用率的正弦曲线2 通过C#指定线程运行的 CPU
- 【转】Linux系统下使用程序控制CPU占用率、内存占用率、网络带宽占用率的方法
- GBAmultiBoot时的Arduino端针脚控制的程序编写~
- vc编写远程控制程序
- BES - 控制进程的CPU占用率
- 要编写一个程序,控制串口通讯,java里面有...
- 使用.Net编写Windows程序,对于窗体控制常见项目
- 使用VB.NET编写控制excel的程序
- 利用a、s、w、d来控制移动 按下空格会发射子弹 最简单的语法用心编写也会有好的程序
- 使用.Net编写Windows程序,对于窗体控制常见项目
- 在AIR编写的Android/iOS程序中控制设备方向
- JAVA控制CPU占用率(编程之美有感)(一)
- 控制CPU占用率曲线
- 16位cpu下主引导扇区及用户程序的编写
- Perl批量杀升腾终端绑定程序进程,解决CPU占用率100%问题。