1

我在某处读到我需要在关闭应用程序时处理对象?

我有这个属性,因为我使用了后台工作程序,所以我在需要调用它们的两个函数中使用它们。

在大多数情况下,当我关闭我的应用程序时没问题,但现在我遇到了这个异常。

private string CpuTextLabelProperty
        {
            set
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(() => CpuTemperature_label.Text = value), null);
                }
            }
            get
            {
                return CpuTemperature_label.Text;
            }

        }

        private Point CpuLocationLabelProperty
        {
            set
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(() => CpuTemperature_label.Location = new Point(210, 200)), null);
                }
            }
            get
            {
                return CpuTemperature_label.Location;
            }
        }

        private string GpuTextLabelProperty
        {
            set
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(() => GpuTemperature_label.Text = value), null);
                }
            }
            get
            {
                return GpuTemperature_label.Text;
            }

        }

        private Point GpuLocationLabelProperty
        {
            set
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(() => GpuTemperature_label.Location= new Point(210,100)), null);
                }
            }
            get
            {
                return GpuTemperature_label.Location;
            }

        }

        private string Label4TextProperty
        {
            set
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(() => label4.Text = value), null);
                }
            }


        }

异常是关于属性:GpuTextLabelProperty里面的get就行了:

返回 GpuTemperature_label.Text;

Win32Exception - 创建窗口句柄时出错

我的 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.BeginInvoke((MethodInvoker)delegate { this.Close(); }); 
            }
        }

如果我需要处理一些东西?如何 ?

这是使用属性的函数:

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());
                                int t = CpuTextLabelProperty.Length;
                                if (t >= 4)
                                {
                                    CpuLocationLabelProperty = new Point(210, 200); // not working to check everything about the locations \\

                                }
                                else
                                {
                                    CpuLocationLabelProperty = new Point(250, 200);
                                }
                                CpuTextLabelProperty = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
                                tempCpuValue = sensor.Value;
                                break;
                            }
                        }
                    }
                }
        }

        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 (GpuTextLabelProperty.Length < 1)
                                        {
                                            if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
                                            {
                                               // Label8 = GpuText;
                                            }
                                        }
                                        else if (UpdatingLabel(sensor.Value.ToString(), GpuTextLabelProperty.Substring(0, GpuTextLabelProperty.Length - 1)))
                                        {
                                          //  Label8 = GpuText;
                                        }
                                        GpuTextLabelProperty = sensor.Value.ToString() + "c";
                                        tempGpuValue = sensor.Value;
                                        //label8.Visible = true;
                                    }

                                    int t = GpuTextLabelProperty.Length;
                                    if (t >= 4)
                                    {
                                        GpuLocationLabelProperty = new Point(210, 100);

                                    }
                                    else
                                    {
                                        GpuLocationLabelProperty = 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 (GpuTextLabelProperty.Length < 1)
                                        {
                                            if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
                                            {
                                             //  Label8 = GpuText;
                                            }
                                        }
                                        else if (UpdatingLabel(sensor.Value.ToString(), GpuTextLabelProperty.Substring(0, GpuTextLabelProperty.Length - 1)))
                                        {
                                           // Label8 = GpuText;
                                        }
                                        GpuTextLabelProperty = sensor.Value.ToString() + "c";
                                        tempGpuValue = sensor.Value;
                                        //label8.Visible = true;
                                    }

                                    int t = GpuTextLabelProperty.Length;
                                    if (t >= 4)
                                    {
                                        GpuLocationLabelProperty = new Point(210, 100);

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

这是完整的异常消息:

System.ComponentModel.Win32Exception was unhandled by user code
  Message=Error creating window handle.
  Source=System.Windows.Forms
  ErrorCode=-2147467259
  NativeErrorCode=87
  StackTrace:
       at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at System.Windows.Forms.Control.CreateHandle()
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.get_WindowText()
       at System.Windows.Forms.Control.get_Text()
       at System.Windows.Forms.Label.get_Text()
       at HardwareMonitoring.Form1.get_GpuTextLabelProperty() in D:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 613
       at HardwareMonitoring.Form1.gpuView() in D:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 412
       at HardwareMonitoring.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 670
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException:
4

1 回答 1

0

当您关闭或退出应用程序时,您必须停止后台工作人员。异常会向您抛出,因为您的代码所依赖的对象正在被操作系统拆除。

请参阅此线程以了解如何执行此操作。

于 2012-08-06T11:34:52.813 回答