-1

问题是,例如 temperature_label 在 47 上,而 label8 也在 47 上。

在下一次迭代中,可以说 temperature_label 下降到 42,所以下一次迭代 label8 也将是 42。但我希望 label8 保持在 47,因为这是最后一个最高值。只有当 temperature_label 超过 47 时才会再次更新 label8。

因此,如果 temperature_label 是相同的值,则没有变化或高于最后一个值更新 label8 。

如果温度标签低于最后一个最高值,不要更新标签8。最后一个最高值我的意思是如果温度标签在70上,现在它在60上很长时间不要更新标签8,但如果它会得到71,更新标签8 a 71并保持71并更新label8 仅当 temperature_label 高于它时。

但是现在代码现在 label8 将得到更新,即使 temperature_label 会从最后一个值下降。

我不想像现在这样用最后一个值更新 label8,而是最后一个最大值。并且仅当最后一个最大值在其上方更改时才更新 label8。

不是当前值,它可能是 temperature_label 和 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;
        }
    }
4

1 回答 1

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 

}
于 2012-08-01T20:39:08.120 回答