2014-03-05 31 views
0

我知道這是什麼意思,但我不明白爲什麼我得到它。 在我的類的構造函數我所做的:爲什麼即時獲取InvalidOperationException?

namespace HM 
{ 
    class Core 
    { 
     public static Form1 form1; 
     Process[] pname; 
     private static List<float?> cpuSensorValues = new List<float?>(); 
     private static List<float?> gpuSensorValues = new List<float?>(); 
     Computer myComputer; 
     Computer computer; 

     public Core(Form1 f) 
     { 
      form1 = f; 
      myComputer = new Computer(); 
      myComputer.CPUEnabled = true; 
      myComputer.FanControllerEnabled = true; 
      myComputer.MainboardEnabled = true; 
      myComputer.Open(); 
      computer = new Computer(); 
      computer.Open(); 
      computer.GPUEnabled = true; 
      OpenHardwareMonitor.Hardware.ISensor isss; 
      Hardwares hwsd; 
      OpenHardwareMonitor.Hardware.ISensor ist; 
      pname = Process.GetProcessesByName("BFBC2Game"); 

     } 

我加了這一點:

pname = Process.GetProcessesByName("BFBC2Game"); 

而當遊戲運行時我看到使用PNAME wontain一個指標是遊戲過程中的斷點。我確定先運行遊戲,然後運行程序。

然後,我有一個方法,我用Form1中的一個計時器來調用它,它可以將日誌文件寫入GPU視頻卡的溫度。 這部分工作很好,直到我添加了這個pname過程。

if (gpuSensorValues.Count == 30 && sensor.Value >= (float)numericupdown) 
             { 
              float a = ComputeStats(gpuSensorValues).Item1; 
              float b = ComputeStats(gpuSensorValues).Item2; 
              float c = ComputeStats(gpuSensorValues).Item3; 
              Logger.Write("********************************"); 
              Logger.Write("GPU Minimum Temperature Is ===> " + a); 
              Logger.Write("GPU Maximum Temperature Is ===> " + b); 
              Logger.Write("GPU Average Temperature Is ===> " + c); 
              Logger.Write("********************************" + Environment.NewLine); 
              gpuSensorValues = new List<float?>(); 
              pname.ToList().ForEach(p => p.Kill()); 

             } 

我加了這一點:

pname.ToList().ForEach(p => p.Kill()); 

我想,例如,如果溫度爲123℃,然後殺死/關閉/立刻關閉遊戲!相反,它不寫入記錄文件的溫度了,它從來沒有殺掉該進程,但寫入日誌文件中的異常消息:

3/5/2014--4:10 AM ==> There was an exception: System.InvalidOperationException: No process is associated with this object. 
    at System.Diagnostics.Process.EnsureState(State state) 
    at System.Diagnostics.Process.EnsureState(State state) 
    at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited) 
    at System.Diagnostics.Process.Kill() 
    at HardwareMonitoring.Core.gpuView(Boolean pause, List`1 myData, Nullable`1 myGpuTemp, Button b1, Decimal numericupdown) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Core.cs:line 172 

這是如果需要完整的方法代碼:

public float? gpuView(bool pause, List<string> myData, float? myGpuTemp, Button b1, decimal numericupdown) 
     { 
      try 
      { 
       if (pause == true) 
       { 
       } 
       else 
       { 


        foreach (var hardwareItem in computer.Hardware) 
        { 
         if (form1.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) 
             { 

              /* else if (UpdatingLabel(sensor.Value.ToString(), label16.Text.Substring(0, label16.Text.Length - 1))) 
              { 
               // Label8 = GpuText; 
              }*/ 
              //myData = new List<string>(); 
              //this.Invoke(new Action(() => data = new List<string>())); 
              if (!form1.IsDisposed) 
              { 
               form1.Invoke(new Action(() => myData.Add("Gpu Temeprature --- " + sensor.Value.ToString()))); 
              } 
              //this.Invoke(new Action(() => listBox1.DataSource = null)); 
              //this.Invoke(new Action(() => listBox1.DataSource = data)); 



              //form1.Invoke(new Action(() => lb1.DataSource = myData)); 
              //sensor.Value.ToString() + "c"; 
              myGpuTemp = sensor.Value; 
              //label8.Visible = true; 
             } 
             //if (sensor.Value > 60) 
             //{ 
             gpuSensorValues.Add(sensor.Value); 
             if (gpuSensorValues.Count == 30 && sensor.Value >= (float)numericupdown) 
             { 
              float a = ComputeStats(gpuSensorValues).Item1; 
              float b = ComputeStats(gpuSensorValues).Item2; 
              float c = ComputeStats(gpuSensorValues).Item3; 
              Logger.Write("********************************"); 
              Logger.Write("GPU Minimum Temperature Is ===> " + a); 
              Logger.Write("GPU Maximum Temperature Is ===> " + b); 
              Logger.Write("GPU Average Temperature Is ===> " + c); 
              Logger.Write("********************************" + Environment.NewLine); 
              gpuSensorValues = new List<float?>(); 
              pname.ToList().ForEach(p => p.Kill()); 

             } 
              b1.Enabled = true; 
             //} 
             //form1.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) 
             { 

              myGpuTemp = sensor.Value; 
              //label8.Visible = true; 
             } 
             if (sensor.Value > 60) 
             { 
              Logger.Write("The Current Ati GPU Temperature Is ===> " + sensor.Value); 
              b1.Enabled = true; 
             } 
             form1.Select(); 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
      catch (Exception err) 
      { 
       Logger.Write("There was an exception: " + err.ToString()); 
      } 
      return myGpuTemp; 
     } 

172行是:

//form1.Select(); 

只是空行沒有什麼可做的。

回答

1

爲Process.Kill的文檔()顯示,如果進程不運行,它可以提高一個InvalidOperationException。

也許嘗試:pname.Where(p => !p.HasExited).ToList().ForEach(p => p.Kill());

0

我可以在你的代碼中看到的唯一問題是你沒有清除pname對象。如果pname引用了不再運行的進程,則會得到此異常。我建議當你想停止它時,獲得新的進程列表。替換以下行

pname.ToList().ForEach(p => p.Kill()); 

pname = Process.GetProcessesByName("BFBC2Game"); 
pname.ToList().ForEach(p => p.Kill()); 
1

的問題是,一個或您pname列表的詳細實例的不是你所期望的狀態。正如在另一個答案中指出的那樣,這可能是因爲該進程沒有運行。另外,其中一個參考可能是null。要防止發生異常,請嘗試添加如下所示的where子句;

pname.ToList().Where(p != null && !p.HasExited).ForEach(p => p.Kill()); 

我不熟悉的Process類,所以我不完全相信你會想用在Where這第二個條件是什麼,但應該給你的想法。這隻會在調用Kill()之前過濾掉列表中會導致錯誤的實例。

相關問題