2012-08-04 58 views
2

我想問題是太多的foreach循環。 但我需要他們得到sensor.Value爲什麼在timer2 tick事件中調用兩個函數時,應用程序運行速度太慢?

這是兩個功能:

private void cpuView() 
     { 

       Computer myComputer = new Computer(); 
       myComputer = new Computer(settings) { CPUEnabled = true }; 

       myComputer.Open(); 
       Trace.WriteLine(""); 
       foreach (var hardwareItem in myComputer.Hardware) 
       { 
        if (hardwareItem.HardwareType == HardwareType.CPU) 
        { 
         hardwareItem.Update(); 
         foreach (IHardware subHardware in hardwareItem.SubHardware) 
          subHardware.Update(); 

         foreach (var sensor in hardwareItem.Sensors) 
         { 
          settings.SetValue("sensor", sensor.Value.ToString()); 
          if (sensor.SensorType == SensorType.Temperature) 
          { 
           sensor.Hardware.Update(); 
           settings.GetValue("sensor", sensor.Value.ToString()); 
           temperature_label.Text = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value"); 
          } 
         } 
        } 
       } 
     } 

而第二個功能:

private void gpuView() 
     { 


       Computer computer = new Computer(); 
       computer.Open(); 
       computer.GPUEnabled = true; 

       foreach (var hardwareItem in computer.Hardware) 
       { 
        if (videoCardType("ati", "nvidia") == true) 
        { 
         HardwareType htype = HardwareType.GpuNvidia; 

         if (hardwareItem.HardwareType == htype) 
         { 

          foreach (var sensor in hardwareItem.Sensors) 
          { 

           if (sensor.SensorType == SensorType.Temperature) 
           { 

            sensor.Hardware.Update(); 
            if (sensor.Value.ToString().Length > 0) 
            { 
             if (newGPULabel.Text.Length < 1) 
             { 
              if (UpdatingLabel(sensor.Value.ToString(), string.Empty)) 
              { 
               label8.Text = newGPULabel.Text; 
              } 
             } 
             else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1))) 
             { 
              label8.Text = newGPULabel.Text; 
             } 
             newGPULabel.Text = sensor.Value.ToString() + "c"; 
             label8.Visible = true; 
            } 

            int t = newGPULabel.Text.Length; 
            if (t >= 4) 
            { 
             newGPULabel.Location = new Point(210, 100); 

            } 
            else 
            { 
             newGPULabel.Location = new Point(250, 100); 
            } 
            timer2.Interval = 1000; 
            if (sensor.Value > 90) 
            { 
             Logger.Write("The current temperature is ===> " + sensor.Value); 
             button1.Enabled = true; 
            } 
            this.Select(); 
           } 
          } 
         } 
        } 
        else 
        { 
         HardwareType htype = HardwareType.GpuAti; 

         if (hardwareItem.HardwareType == htype) 
         { 

          foreach (var sensor in hardwareItem.Sensors) 
          { 

           if (sensor.SensorType == SensorType.Temperature) 
           { 

            sensor.Hardware.Update(); 
            if (sensor.Value.ToString().Length > 0) 
            { 
             if (newGPULabel.Text.Length < 1) 
             { 
              if (UpdatingLabel(sensor.Value.ToString(), string.Empty)) 
              { 
               label8.Text = newGPULabel.Text; 
              } 
             } 
             else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1))) 
             { 
              label8.Text = newGPULabel.Text; 
             } 
             newGPULabel.Text = sensor.Value.ToString() + "c"; 
             label8.Visible = true; 
            } 

            int t = newGPULabel.Text.Length; 
            if (t >= 4) 
            { 
             newGPULabel.Location = new Point(210, 100); 

            } 
            else 
            { 
             newGPULabel.Location = new Point(250, 100); 
            } 
            timer2.Interval = 1000; 
            if (sensor.Value > 90) 
            { 
             Logger.Write("The current temperature is ===> " + sensor.Value); 
             button1.Enabled = true; 
            } 
            this.Select(); 
           } 
          } 
         } 
        } 
      } 
     } 

而在定時器Tick事件:

private void timer2_Tick(object sender, EventArgs e) 
     { 

      gpuView(); 
      cpuView(); 


     } 

如果我不在tick事件中調用這個函數,程序運行平穩b即使即時即時只調用其中一個功能,每n秒停留一次。

由於即時更新cpu和gpu溫度,我想快速更新它們,timer2設置爲間隔100。 我不確定問題是功能中的foreach循環太多了,或者間隔爲100,所以它試圖從傳感器獲得值太快,所以硬件無法過快地處理信息。

也許有一種方法可以刪除一些foreach循環? 我不想將所有的Form1代碼上傳到這裏。但所有的snes和硬件都與OpenHardwareMonitorApplication的OpenHardwareMonitor.dll連接。

回答

0

您可以使用BackgroundWorker的,它允許你做上多線程簡單的方法。 它會對你的情況有用,並以異步方式調用doWork方法。在這種方法中,你可以在循環中調用你的兩個函數。 這裏msdn解釋http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx 它比定時器,你設置一個隨機間隔值,因爲你必須讓你的功能完成,所以如果一個事件必須發生它不會...

+0

高興地幫助你 – 2012-08-04 10:36:30

0

如果您希望應用程序再次順利運行,您可以使用Application.DoEvents();無效,ehich使應用程序準備高需求的代碼。

樣品,在你的Tick事件:

private void timer2_Tick(object sender, EventArgs e) 
        { 
      Application.DoEvents(); 
            gpuView(); 
            cpuView(); 


        } 

或者您也可以撥打GpuView和CpuViews線程,使用的System.Threading命名空間

相關問題