我在Form1中有这段代码:
private string CpuTextLabelProperty
{
set
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => CpuTemperature_label.Text = value), null);
}
}
get
{
return CpuTemperature_label.Text;
}
}
我在这里两次使用这个属性:
private void cpuView()
{
if (pauseContinueDoWork == true)
{
}
else
{
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());
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;
if (sensor.Value > 60)
{
Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
button1.Enabled = true;
}
int t = CpuTextLabelProperty.Length;
if (t >= 4)
{
CpuLocationLabelProperty = new Point(50, 50); // not working to check everything about the locations \\
}
else
{
CpuLocationLabelProperty = new Point(50, 50);
}
break;
}
}
}
}
}
}
我在这里使用的cpuView()方法:
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;
blinking_label();
NudgeMe();
}
else
{
soundPlay = false;
stop_alarm = true;
}
cpuView();
gpuView();
Thread.Sleep(1000);
}
}
}
我有一名背景工作者。
现在当我退出应用程序时:
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
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
backgroundWorker1.CancelAsync();
}
}
}
当我退出应用程序并在大多数情况下单击是时获取或win32异常或此RaceOnRCWCleanup异常有时在大多数情况下没有错误我得到这两个例外。
我该如何解决?
现在我尝试退出并在Form1.Designer.cs上获得另一个例外:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
在线:base.Dispose(disposing);错误是:invalidOperationException:在执行CreateHandle()时无法调用值Dispose()
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Value Dispose() cannot be called while doing CreateHandle().
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.Windows.Forms.Label.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.Windows.Forms.Form.Dispose(Boolean disposing)
at HardwareMonitoring.Form1.Dispose(Boolean disposing) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.Designer.cs:line 20
at System.ComponentModel.Component.Dispose()
at System.Windows.Forms.Form.WmClose(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
编辑:
现在我再次获得了CpuTextLabelProperty上的win32异常:
return CpuTemperature_label.Text;
创建窗口句柄时出错
System.ComponentModel.Win32Exception was unhandled by user code
HResult=-2147467259
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_CpuTextLabelProperty() in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 630
at HardwareMonitoring.Form1.cpuView() in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 391
at HardwareMonitoring.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 739
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException: