问题在于,例如,temperature_label为47,而label8也为47。
在下一次迭代中,我们可以说temperature_label下降到42,所以下一次标记label8也将是42.但我希望label8将保持在47,因为这是最后一个最高值。只有当temperature_label达到47以上时才会再次更新label8。
因此,如果temperature_label是相同的值,则不会更改或高于最后一个值更新label8。
如果temperature_label低于最后一个最高值,则不要更新label8.Last最高值我的意思是如果temperature_label是70并且现在它长时间60不更新label8但是如果它将获得71更新label8 a 71并保留它71并且只有在temperature_label高于它时才更新label8。
但是现在代码现在,即使temperature_label从最后一个值下降,label8也会更新。
我不想像现在这样使用最后一个值更新label8,而是最后一个最高值。并且只有在最后一个最高值更改时才更新label8。
不是当前值,温度标签和label8将具有相同的值,但如果temperature_label下拉,则label8不会。如果temperature_lable从最后一个最高值开始,则再次更新label8。这是我需要的条件。
代码:
即时出现异常错误:if(UpdatingLabel(sensor.Value.ToString(),temperature_label.Text.Substring(0,temperature_label.Text.Length - 1)))
例外是:ArgumentOutOfRangeException:
长度不能小于零。 参数名称:长度
System.ArgumentOutOfRangeException was unhandled
Message=Length cannot be less than zero.
Parameter name: length
Source=mscorlib
ParamName=length
StackTrace:
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at Bursa.Form1.timer2_Tick(Object sender, EventArgs e) in D:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 369
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Bursa.Program.Main() in D:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我根据您的代码所做的更改后的代码:
private void timer2_Tick(object sender, EventArgs e)
{
Computer computer = new Computer();
computer.Open();
computer.GPUEnabled = true;
foreach (var hardwareItem in computer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
if (UpdatingLabel(sensor.Value.ToString(), temperature_label.Text.Substring(0, temperature_label.Text.Length - 1)))
label8.Text = temperature_label.Text;
temperature_label.Text = sensor.Value.ToString() + "c";
label8.Visible = true;
int t = temperature_label.Text.Length;
if (t > 3)
{
temperature_label.Location = new Point(238, 200);
}
timer2.Interval = 1000;
if (sensor.Value > 90)
{
Logger.Write("The current temperature is ===> " + sensor.Value);
button1.Enabled = true;
}
this.Select();
}
}
}
}
}
private bool UpdatingLabel(string newVal, string oldVal)
{
int intVal1 = int.Parse(newVal);
int intVal2 = int.Parse(oldVal);
if (intVal1 > intVal2)
return true;
else
return false;
}
}
答案 0 :(得分:1)
在更新之前,只需获取temperature_label的当前值,并将其设置为Label8
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
label8.Text = temperature_label.Text
temperature_label.Text = sensor.Value.ToString()+ "c";
label8.Visible = true;
int t = temperature_label.Text.Length;
//remaining code
}