我刚刚关闭了退出的应用程序,并收到Win32异常错误创建窗口句柄可能会这样做



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

我有这个属性,我在拖曳函数中使用它们,我需要调用它们,因为我使用后台工作者。

在大多数情况下,当我关闭我的应用程序时,还可以,但现在我得到了这个异常。

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);
                }
            }

        }

例外是在属性上:GpuTextLabel属性在获取行中:

返回GpuTemperature_label。发短信;

Win32异常 - 创建窗口句柄时出错

我的 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-SharpHardwareMonitoringHardwareMonitoringHardwaremonitoringForm1.cs:line 613
       at HardwareMonitoring.Form1.gpuView() in D:C-SharpHardwareMonitoringHardwareMonitoringHardwaremonitoringForm1.cs:line 412
       at HardwareMonitoring.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:C-SharpHardwareMonitoringHardwareMonitoringHardwaremonitoringForm1.cs:line 670
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException:
关闭

或退出应用程序时,必须停止后台工作线程。异常会抛给你,因为你的代码所依赖的对象正在作系统拆除。

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

最新更新