1

在 Form1 我有这个代码:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                this.backgroundWorker1.CancelAsync();                
            }


        }

然后我上了一个新课:

class Core
    {
        public static Form1 form1;

        public Core(Form1 f)
        {
            form1 = f;

        }

        public static float? cpuView(bool pause , CpuTemperature cpuTemp , Form1 f1 , List<string> myData , float? myCpuTemp , Button b1)
        {
            if (pause == true)
            {
            }
            else
            {
                Computer myComputer = new Computer();
                myComputer = new Computer(cpuTemp)
                {
                    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)
                        {
                            cpuTemp.SetValue("sensor", sensor.Value.ToString());
                            if (sensor.SensorType == SensorType.Temperature)
                            {
                                sensor.Hardware.Update();
                                cpuTemp.GetValue("sensor", sensor.Value.ToString());                                
                                f1.Invoke(new Action(() => myData.Add("Cpu Temeprature --- " + sensor.Value.ToString())));
                                myCpuTemp = sensor.Value;
                                if (sensor.Value > 60)
                                {


                                    Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
                                    b1.Enabled = true;
                                }

                                break;
                            }
                        }
                    }
                }
            }
            return myCpuTemp;
        }

异常就行了:

f1.Invoke(new Action(() => myData.Add("Cpu Temeprature --- " + sensor.Value.ToString())));

我认为造成问题的已处置对象是 Form1 它自己。

System.ObjectDisposedException was unhandled by user code
  HResult=-2146232798
  Message=Cannot access a disposed object.
Object name: 'Form1'.
  Source=System.Windows.Forms
  ObjectName=Form1
  StackTrace:
       at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
       at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
       at System.Windows.Forms.Control.Invoke(Delegate method)
       at HardwareMonitoring.Core.cpuView(Boolean pause, CpuTemperature cpuTemp, Form1 f1, List`1 myData, Nullable`1 myCpuTemp, Button b1) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Core.cs:line 55
       at HardwareMonitoring.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 427
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException: 

那么处置什么呢?因为我没有在我的代码中做任何处理()它的事情。

也许问题出在Form1,因为我在Form1中使用了一个backgroundworker,而backgroundworker正在使用这个类,如果我关闭程序,那么form1被释放,但变量f1可能还在尝试做某事?

我该如何解决?

编辑**

更改了 cpuView 功能:

public static float? cpuView(bool pause , CpuTemperature cpuTemp , Form1 f1 , List<string> myData , float? myCpuTemp , Button b1)
        {
            try
            {
                if (pause == true)
                {
                }
                else
                {
                    Computer myComputer = new Computer();
                    myComputer = new Computer(cpuTemp)
                    {
                        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)
                            {
                                cpuTemp.SetValue("sensor", sensor.Value.ToString());
                                if (sensor.SensorType == SensorType.Temperature)
                                {
                                    sensor.Hardware.Update();
                                    cpuTemp.GetValue("sensor", sensor.Value.ToString());
                                    if (!f1.IsDisposed)
                                    {
                                        f1.Invoke(new Action(() => myData.Add("Cpu Temeprature --- " + sensor.Value.ToString())));
                                    }
                                    myCpuTemp = sensor.Value;
                                    if (sensor.Value > 60)
                                    {


                                        Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
                                        b1.Enabled = true;
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception err)
            {
                return null;
            }
            return myCpuTemp;
        }

在调用行之前添加了一个检查,还添加了 try 和 catch。那样行吗 ?或者我应该为 cpuView 和 gpuView 做另一种方式?

编辑**

这是 Form1 中的 backgroundworker dowork 事件:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {

                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
                    {
                        soundPlay = true;
                        NudgeMe();
                    }
                    else
                    {
                        soundPlay = false;
                        stop_alarm = true;

                    }

                    tempCpuValue = Core.cpuView(pauseContinueDoWork,cpu,this,data,tempCpuValue,button1);
                    tempGpuValue = Core.gpuView(pauseContinueDoWork,data,tempGpuValue,button1);
                    this.Invoke(new Action(() => data = new List<string>()));
                    tempCpuValue = Core.cpuView(pauseContinueDoWork, cpu, this, data, tempCpuValue, button1);
                    tempGpuValue = Core.gpuView(pauseContinueDoWork, data, tempGpuValue, button1);
                    this.Invoke(new Action(() => listBox1.DataSource = null));
                    this.Invoke(new Action(() => listBox1.DataSource = data));                    
                }
            }
        }
4

1 回答 1

1

在调用表单之前检查表单是否已处理。

if (!f1.IsDisposed)
{
    f1.Invoke(new Action(() => myData.Add("Cpu Temeprature --- " + sensor.Value.ToString())));
}

此外,在您的一些 for 循环中,您应该添加取消检查,以便后台工作人员更快地完成。

if (CancellationPending) return;

即使这样,您仍然有可能得到已处理的异常。IsDisposed如果表单在检查之后和调用之前关闭Invoke,那么您将收到异常。对于这种罕见的情况,添加错误处理并忽略它(这是一个在应用程序关闭时可以安全忽略的异常)。

于 2013-09-24T03:29:28.207 回答