C# 如何显示最高温度和最低温度?

C# 如何显示最高温度和最低温度?,c#,winforms,C#,Winforms,我的程序每x秒显示一次GPU视频卡的温度。 我想它会显示每一次的最低温度和最高温度 例如,温度是:49摄氏度 然后它又变为51,然后变为45,然后又变为49 所以最大值是51,最小值是45。 如果温度将升高到51以上,则最大值将更改为51。 最低限度也一样 private void timer_Tick(object sender, EventArgs e) { if (textBox3.Text == "") {

我的程序每x秒显示一次GPU视频卡的温度。 我想它会显示每一次的最低温度和最高温度

例如,温度是:49摄氏度 然后它又变为51,然后变为45,然后又变为49

所以最大值是51,最小值是45。 如果温度将升高到51以上,则最大值将更改为51。 最低限度也一样

private void timer_Tick(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                timer.Stop();
                MessageBox.Show("אנא הכנס נתונים בתיבות הטקסט מצד שמאל כדי להמשיך");
            }
            else
            {
                timer.Start();
                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();
                                //textBox1.Text = String.Format("The current temperature is {0}", sensor.Value);
                                temperature_label.Text = sensor.Value.ToString() + "c";
                                label8.Text = sensor.Value.ToString() + "c"; // to save the last maximum temperature to a external variable and then show it in label8 same for the minimum temperature \\
                                label8.Visible = true;
                                int t = temperature_label.Text.Length;
                                if (t > 3)
                                {
                                    temperature_label.Location = new Point(238, 200);
                                }
                                timer.Interval = 1000;
                                if (sensor.Value > float.Parse(textbox3_value))
                                {
                                    Logger.Write("The current temperature is ===> " + sensor.Value);
                                    button1.Enabled = true;
                                    //  temperature_label.ForeColor = Color. // to check wich colors to use blue and red regular state and in emergency when its over 90c ?! what colors for each label ?
                                }
                                this.Select();
                            }
                        }
                    }
                }
            }
        }
我有一个计时器滴答声事件,我更新了温度。 在label8中,我想显示最大值。
现在,它只是实时显示温度。

您可能需要三个标签和三个变量:

  // Example
  tempCurrent = sensor.Value;
  if (tempMax < tempCurrent)
    tempMax = tempCurrent;
  if (tempMin > tempCurrent)
    tempMin = tempCurrent;

  lblMax.Text = tempMax + "c";
  lblMin.Text = tempMin + "c";
  lblCurrent.Text = tempCurrent + "c";
//示例
温度电流=传感器值;
if(最大温度<当前温度)
tempMax=tempCurrent;
如果(tempMin>tempCurrent)
tempMin=tempCurrent;
lblMax.Text=tempMax+“c”;
lblMin.Text=tempMin+“c”;
lblCurrent.Text=tempCurrent+c;

只需添加两个变量来存储最高和最低温度:

double minTemp = Double.MaxValue,maxTemp = Double.MinValue;
以及:

double value=Convert.ToDouble(传感器值);
如果(值最大温度)最大温度=值,则为else;
lblMin.Text=minTemp.ToString()+“c”;
lblMax.Text=maxTemp.ToString()+“c”;

愚人节不是今天,对吧?不如用
Int.MAX\u值
Int.MIN\u值
代替
bool
来控制第一个值?好。现在..根据OP,
sensor.Value
已经返回了一个数字,它似乎是一个
float
…然后我们可以使用float.MinValue和float.MaxValue,或者更好:double=)为什么
double
而不是
float
?因为我不知道返回值的类型,double的范围比float大,虽然我们谈论温度,但无论如何也不能太像那样
double value = Convert.ToDouble(sensor.Value);

if(value < minTemp) minTemp = value;
else if(value > maxTemp) maxTemp = value;

lblMin.Text =  minTemp.ToString() + "c";
lblMax.Text =  maxTemp.ToString() + "c";