`
haierboos
  • 浏览: 434438 次
文章分类
社区版块
存档分类
最新评论

Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率C#

 
阅读更多

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5160822

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8678382

欢迎关注微博:http://weibo.com/MoreWindows

Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#

http://blog.csdn.net/morewindows/article/details/8678382

前面《Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率》介绍了在Windows XPWindows 7系统下获取CPU整体使用率,而目前大多数CPU都是多核CPU,因此本文来介绍如何获取多核CPU下各核的使用率

Windows系统CPU内存网络性能统计博客目录:

1Windows系统CPU内存网络性能统计第一篇内存

http://blog.csdn.net/morewindows/article/details/8459219

2Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率

http://blog.csdn.net/morewindows/article/details/8678359

3Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#

http://blog.csdn.net/morewindows/article/details/8678382

4Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++

http://blog.csdn.net/morewindows/article/details/8678396

使用C#来获取多核CPU下各核的使用率是非常方便的。短短几行就能搞定,并且程序能正常运行于Windows XPWindows 7系统中。下面先给出代码。

//Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#
// 经过测试,可以在WinXP及Win7下使用
//http://blog.csdn.net/morewindows/article/details/8678382
using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("    Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#");
            Console.WriteLine(" - http://blog.csdn.net/morewindows/article/details/8678382 -");
            Console.WriteLine(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n"); 

            PerformanceCounter[] counters = new PerformanceCounter[ System.Environment.ProcessorCount ]; 
            for(int i = 0; i < counters.Length; i++)        
            {            
                counters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString()); 
            }        
            while(true)        
            {          
                for(int i = 0; i < counters.Length; i++)       
                {             
                    float f = counters[i].NextValue();   
                    Console.Write("CPU-{0}: {1:f}%  ", i + 1, f);     
                }          
                Console.WriteLine();        
                Thread.Sleep(1000);       
            }   
        }
    }
}

由代码可以看出,主要是使用了System.Diagnostics中的PerformanceCounter类。这个类的功能强大,可以查阅MSDN得到详细解释。

WinXP系统运行结果如下(本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5160822):

Win7系统运行结果如下(本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5160822):

由本篇《Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#》(http://blog.csdn.net/morewindows/article/details/8678382)可以看出,C#无疑是一门很方便的语言,对C/C++程序员来说,了解下C#是非常有帮助的。

下一篇《Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++》(http://blog.csdn.net/morewindows/article/details/8678396)将介绍使用C++代码来引用C#编写的DLL来完成在C++下获取多核CPU各核使用率。

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5160822

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8678382

欢迎关注微博:http://weibo.com/MoreWindows


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics