我知道這是什麼意思,但我不明白爲什麼我得到它。 在我的類的構造函數我所做的:爲什麼即時獲取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();
只是空行沒有什麼可做的。