2012-09-01 122 views
5

我正在編寫一個程序來監視計算機的各種資源,如CPU使用率等。我想監視GPU使用情況(GPU負載,而不是溫度)。如何讀取GPU負載?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Diagnostics; 
using DannyGeneral; 


namespace CpuUsage 
{ 
    public partial class Form1 : Form 
    { 
     private bool processEnded; 
     private Process[] processList; 
     private string timeStarted; 
     private string timeEnded; 
     private List<float> processValues; 
     private bool alreadyRun; 
     private DateTime dt; 
     private DateTime dt1; 
     private PerformanceCounter theCPUCounter; 
     private PerformanceCounter theMemCounter; 
     private PerformanceCounter specProcessCPUCounter; 
     private float cpuUsage; 
     private float memUsage; 
     private List<float> Values; 

     public Form1() 
     { 
      InitializeComponent(); 

      processEnded = false; 
      processList = Process.GetProcesses(); 
      for (int i = 0; i < processList.Count(); i++) 
      { 
       listView1.Items.Add(processList[i].ProcessName); 
      } 
      processValues = new List<float>(); 
      alreadyRun = false; 
      dt = DateTime.Now; 


      Values = new List<float>(); 
       theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); 
       theMemCounter = new PerformanceCounter("Memory", "Available MBytes"); 
       specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName); 

       Logger.Write("The Program Started At *** " + DateTime.Now.ToShortTimeString()); 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      memUsage = theMemCounter.NextValue(); 
      label1.Text = memUsage.ToString(); 
      Logger.Write("Memory Usage " + memUsage.ToString()); 
      cpuUsage = this.theCPUCounter.NextValue(); 
      label2.Text = cpuUsage.ToString(); 
      Logger.Write("Cpu Usage " + this.cpuUsage.ToString()); 
      Values.Add(cpuUsage); 
      isProcessRunning(); 
      if (alreadyRun == true) 
      { 
       processValues.Add(cpuUsage); 
      } 

     } 
+2

對於將來的問題,請考慮只提供相關代碼的小樣本。你的當前版本有太多的UI相關的東西,其中簡單的'Console.Write(「CPU使用率:{0}」,新的PerformanceCounter(「Processor」,「%Processor Time」,「_Total」); .NextValue() )'就夠了。 –

回答

3

video card's own CPU被稱爲GPU

查找與該或video性能計數器(使用Start->Run->PerfMon或輸入在Start菜單的搜索框中PerfMon;在圖形上單擊鼠標右鍵,選擇Add Counter...)。

如果您的視頻卡製造商沒有提供GPU的任何性能計數器,那麼您沒有任何獲得。

+0

當然,性能計數器並不是關於正在運行的系統的唯一信息來源。 –

+0

@BenVoigt:我從來沒有打算說他們是。我建議,作爲一個來源,基於其他代碼發佈。隨意提供其他人。 :-) –

+0

我不確定你的最後一句話意味着什麼。在任何情況下,都有RivaTuner,它帶有一個以編程方式訪問數據的API,以及許多其他監控程序,它們在沒有性能計數器的情況下獲取這些數據。 –