【接續上一篇 [Code] C# 效能監視器 - 取得 CPU 使用率之應用(上)

上次我們做到了如何取得正在使用中的程序 (_instanceName),但離我們最後一步取得CPU使用率還差一點點。因此最後我們必須試圖去取得該程序的計數器(_counter),如同上一篇說到的,在效能監視器中也有很多個可以使用的功能,我們必須先知道有哪些可以選擇,再依照我們的需求來使用。

藉著 PerformanceCounterCategory 中的 GetCounters() 方法,加上原先取得的 instanceName,我們可以獲得該 instance 的 counter,程式碼如下

        private static void GetCountersOfInstance(String _category, String _instanceName)
        {
            PerformanceCounterCategory counterCategory = new PerformanceCounterCategory(_category);
            PerformanceCounter[] counters = counterCategory.GetCounters();
            foreach (PerformanceCounter counter in counters)
            {
                Console.WriteLine("\t"+counter.CounterName);
            }
        }

        /****************************************/
        foreach (String name in instanceNames) // The code in last article
        {
           GetCountersOfInstance(_category , name );
        }
        /**********************************************/

如此就可以看到該 Instance 底下所可以使用的計數器了(如圖),而其中 "% Processor Time" 就是,其他包括記體使用量、檔案使用量等如果未來有機會再介紹(不過應該機率不大)。

as_5.PNG 

當知道了我們要的 Category (類別) 與 Instance (執行個體) 以及 Counter (計數器) 後,離目標也不遠了。若只要取得單一個程序(執行個體)的 CPU 使用率,直接使用 PerformanceCounter 的建構子,將三個資料代入即可。如果是要取得所有程序的 CPU 使用率,就多使用 for 或者 foreach 把所有的數值取得,以下為整段取得執行程序 CPU 使用率的程式碼。

可使用函數有:

GetCategoryName() - 取得電腦可用之所有分類名稱
GetCountersOfInstance(String _category, String _instanceName) - 輸入分類與執行個體名稱,取得可用之計數器
GetDetailOfProcess(String _categoryName , String _counterName , String _processName) - 輸入分類、執行個體名稱以及計數器名稱取得數值
GetInstanceNameOfCategory(String _categoryName) - 主要使用函式

以上自行變化,並未寫多載。 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;

namespace tSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // GetCategoryName();
            // GetInstanceNameOfCategory("Process");  // Category => Process ( to get the detail information and function of process )
            Console.Read();
        }
        
        private static void GetCategoryName()
        {
            PerformanceCounterCategory[] performanceCounterCategory;
            performanceCounterCategory = PerformanceCounterCategory.GetCategories();
            foreach (PerformanceCounterCategory pCounterCategory in performanceCounterCategory)
            {
                Console.WriteLine("\t["+pCounterCategory.CategoryName+"]");
            }
        }

        private static void GetCountersOfInstance(String _category, String _instanceName)
        {
            PerformanceCounterCategory counterCategory = new PerformanceCounterCategory(_category);
            PerformanceCounter[] counters = counterCategory.GetCounters();
            foreach (PerformanceCounter counter in counters)
            {
                Console.WriteLine("\t"+counter.CounterName);
            }
        }

        static int serial;
        private static void GetDetailOfProcess(String _categoryName , String _counterName , String _processName)
        {
            serial++;
            PerformanceCounter performanceCounter = new PerformanceCounter(_categoryName, _counterName, _processName );
            Console.WriteLine("\t "+serial+".  [ " + _processName + " ] : " + performanceCounter.NextValue());
        }

        private static void GetInstanceNameOfCategory(String _categoryName)
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory(_categoryName); // Get the instance name of category in perfomance counter
            String[] instanceNames;  // Store the instance's name
            PerformanceCounter[] counters;
            try
            {
                instanceNames = category.GetInstanceNames();

                if (instanceNames.Length == 0)
                {
                    counters = category.GetCounters();
                }
                else  // if the category exist the instances , store its' name 
                {
                    serial = 0;
                    foreach (String name in instanceNames)
                    {
                        //GetCountersOfInstance(_categoryName, name);
                        GetDetailOfProcess(_categoryName, "% Processor Time", name);
                    }
                    
                }
            }
            catch (InvalidOperationException invalidOpreationException)
            {
                Console.WriteLine(invalidOpreationException);
            }
            catch (Win32Exception wException)
            {
                Console.WriteLine(wException);
            }
        }
    }
}

補上執行結果圖。

as_6.PNG 

arrow
arrow
    全站熱搜

    Kerash Thieph 發表在 痞客邦 留言(1) 人氣()