當前位置:才華齋>計算機>計算機硬體>

利用C#監控計算機CPU的使用率

計算機硬體 閱讀(1.17W)

今天在網上看到一段比較好玩的程式碼,用來顯示本地計算機CPU的使用情況,整理如下,歡迎使用,更多資訊請瀏覽應屆畢業生考試網!

利用C#監控計算機CPU的使用率

using System;

using nostics;

using ading;

namespace ConsoleApplication1

{

class Program

{

private const string CategoryName = "Processor";

private const string CounterName = "% Processor Time";

private const string InstanceName = "_Total";

private static void OutPut(string txt)

{

eLine(txt);

}

[STAThread]

public static void Main()

{

PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);

OutPut("----------------------------開始創造效能計數器----------------------------");

OutPut("計數器型別:" + terType);

OutPut("計數器的`說明:" + terHelp);

OutPut("開始輸出:");

while (true)

{

p(1000);

float cpuLoad = Value();

OutPut("CPU 耗用:" + cpuLoad + "%");

}

}

}

}

直接執行程式,對比控制檯輸出的結果與計算機的Windows工作管理員中顯示的CPU使用值,會發現原來Windows工作管理員顯示的值是CPU使用率的四捨五入的結果。有興趣的朋友可以自己複製程式碼試試看啦。