I read somewhere that i need to dispose objects when closing the application ?
I have this properties i did to use them in tow functions i needed to invoke them since im using a backgroundworker.
In most of the times when i closed my application it was ok but now i got this exception.
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);
}
}
}
The exception is on the property: GpuTextLabelProperty inside the get on the line:
return GpuTemperature_label.Text;
Win32Exception – Error creating window handle
My Form1 closing event:
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(); });
}
}
If i need ot dispose something wich ones ? And how ?
This is the functions im using the properties:
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();
}
}
}
}
}
}
And this is the full exception message:
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:
You have to stop your background worker when you close or exit your application. The exceptions gets thrown at you because the objects your code is relying on are being torn down by the OS.
See this thread on how to do it.