2012-07-31 52 views
0

現在,它顯示一次溫度。 我希望它能像openhardwaremonitor原創程序那樣工作,即溫度每秒更新/更新或根據溫度是否有任何變化進行更新,我不確定它在那裏如何工作。使用OpenHardwareMonitor庫的輸出每n秒更新一個文本框

我所做的是從構造函數中取出所有的foreach循環,並將其放入計時器tick事件中。但它的工作。在openhardwaremonitor中,它的顯示更改爲51c,而在我的應用程序中它始終在44c上,就像我第一次運行該應用程序一樣。

我在textBox行上使用了一個斷點,有時它在那裏有時只是在執行內部循環,但dosent會通過if和dosent進入消息箱,如果確實進入了消息箱,它仍然會顯示44c。

代碼:

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.Management; 
using OpenHardwareMonitor.Hardware; 

namespace NvidiaTemp 
{ 
    public partial class Form1 : Form 
    { 
     Computer computer = new Computer(); 
     public Form1() 
     { 
      InitializeComponent(); 

      computer.Open(); 
      computer.GPUEnabled = true; 
      timer1.Enabled = true; 





     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      foreach (var hardwareItem in computer.Hardware) 
      { 

       if (hardwareItem.HardwareType == HardwareType.GpuNvidia) 
       { 
        foreach (var sensor in hardwareItem.Sensors) 
        { 
         if (sensor.SensorType == SensorType.Temperature) 
         { 
          // MessageBox.Show(String.Format("The current temperature is {0}", sensor.Value)); 
          textBox1.Text = String.Format("The current temperature is {0}", sensor.Value); 
         } 
        } 
       } 

      } 
     } 
    } 
} 
  1. 爲什麼它dosent更新dosent到文本框中行其每個做循環時間?

  2. 如果溫度值發生變化或者在openhardwaremonitor原始程序中使用定時器工作,是否更新溫度?

+0

*提示*:使用linq代替每個嵌套。 另外...對不起,但我真的不明白你想問什麼。 – Yoeri 2012-07-31 22:34:28

回答

1

我實際上在您的earlier question上發佈了部分回答,但在此處僅供參考。

你的問題其實大多隻是Windows.Forms.Timer標準問題(或者你甚至可以用一個BackgroundWorker或您選擇與檢查的通常警告InvokeRequired任何其他定時器)

無論如何,以下是什麼,應該爲你工作很短的例子:

Timer timer; 

    Computer myComputer; 

    ISensor GPUTemperatureSensor; 

    public Form1() 
    { 

     InitializeComponent(); 

     myComputer = new Computer(); 

     myComputer.Open(); 

     myComputer.GPUEnabled = true; 

     foreach (var hardwareItem in myComputer.Hardware) 
     { 

      if (hardwareItem.HardwareType == HardwareType.GpuNvidia) 
      { 
       foreach (var sensor in hardwareItem.Sensors) 
       { 
        if (sensor.SensorType == SensorType.Temperature) 
        { 
         GPUTemperatureSensor = sensor; 

        } 
       } 
      } 

     } 

     timer = new Timer(); 
     timer.Interval = 5000; 
     timer.Tick += new EventHandler(timer_Tick); 
     timer.Start(); 

    } 

    void timer_Tick(object sender, EventArgs e) 
    { 
     if(GPUTemperatureSensor != null) 
     { 
      GPUTemperatureSensor.Hardware.Update();//This line refreshes the sensor values 
      textBox1.Text = String.Format("The current temperature is {0}", GPUTemperatureSensor.Value); 
     } 
     else 
     { 
      textBox1.Text = "Could not find the GPU Temperature Sensor. Stopping."; 
      timer.Stop(); 
     } 
    } 

我在類級別實際存儲的傳感器,以避免枚舉硬件收集每次。

我還在硬件上包含了@mikez對Update()方法的評論 - 謝謝Mike!

+0

啊......再次看看我的第一個例子 - 我已經在構造函數中聲明瞭一個名爲「ISensor GPUTemperatureSensor」的類級別變量 - 我們將該變量分配給GPU溫度傳感器,然後我們可以在所有非在形式的靜態方法... – dash 2012-07-31 23:13:21

+0

破折號我現在嘗試您的第一個代碼替換textBox行傳感器與GPUTemperatureSensor.Value所以沒有錯誤我運行它,它顯示溫度第一次,但它劑量更新它。它一直在第一個臨時45c。 – user1544479 2012-07-31 23:13:57

+0

短跑在你的地方工作嗎?每5秒左右溫度變化一次? – user1544479 2012-07-31 23:14:34

0

我不確定你爲什麼要這樣做在循環中。我猜想這是造成問題的原因,因爲如果在下一次打勾之前循環未完成,他們可能會彼此踩踏。

假設只有1 GPU和GPU是1個溫度傳感器,爲什麼不這樣做:

var hardwareItem = computer.Hardware.Single(h => h.HardwareType == HardwareType.GpuNvidia) 
var sensor = hardwareItem.Sensors.Single(s => s.SensorType == SensorType.Temperature) 
// MessageBox.Show(String.Format("The current temperature is {0}", sensor.Value)); 
textBox1.Text = String.Format("The current temperature is {0}", sensor.Value); 

您將完全消除不必要的循環和應該作出這樣的方法跑得快。

+0

在OP的文章中沒有什麼需要循環。我只是不確定爲什麼需要循環將1個對象從硬件列表中拉出,然後從列表中移出1個傳感器。 – 2012-07-31 23:13:17

+0

這只是枚舉每次硬件列表 - 這是一些示例代碼;您的代碼是枚舉的整齊收縮,因爲.Single仍會枚舉項目 - 它僅返回第一個GpuNvidia項目,因此它不一定遍歷所有項目。硬件可能會充滿大量不同類型的硬件。 – dash 2012-07-31 23:15:20

+0

傑夫你是正確的..但我讀它,因爲他想要一個基於循環的硬件列表再次含糊不清的問題描述...大聲笑 – MethodMan 2012-07-31 23:21:34