如何在 C# 中计算时间差(减去时间)?

How to calculate time difference (subtract time) in C#?

我正在创建程序以使用 Arduino 测量真空值并以使用 C# 创建的形式显示它。 我想将时间存储为常数。这是节目的开始时间。我给它分配了“连接”按钮。当我点击时,时间值正在存储。 然后我使用“计时器滴答”方法立即查看测量值。此外,DateTime.Now 显示即时系统时间。它像时钟一样在变化。 click here to see the picture

这是“连接”按钮的代码;

public void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;

        try
        {
            if (comboBox1.Text == "")

            {
                MessageBox.Show("Please select the port name!");
            }
            else
            {
                serialPort1.PortName = comboBox1.Text;
                serialPort1.ReadBufferSize = 8;
                serialPort1.Open();
                timeval.Clear();
                button1.Enabled = false;
                button2.Enabled = true;

                timer1.Start();

                DateTime myDateTime = DateTime.Now;  //It stores the instant time information when button is clicked.
                label14.Text = myDateTime.ToString(); // shows in the label

                
                //serialPort1.ReadTimeout = 300;

            }
          
        }
        catch (UnauthorizedAccessException)
        {
            MessageBox.Show("Unauthorized Access!");
        }
    }

这是定时器滴答的代码;

   public void timer1_Tick(object sender, EventArgs e)
    {
        label12.Text = DateTime.Now.ToString();

        //TimeSpan time_difference = DateTime.Now - myDateTime; // trying to calculate time difference.

        //double saniye = time_difference.Seconds;

        //double dakika = time_difference.Minutes;

        //label10.Text = (Math.Round(saniye)).ToString();
        //label16.Text = (Math.Round(dakika)).ToString();

        new_data = 756 * (float.Parse(data) - 1023) / 1023;
        sensorval.Add(Math.Round(new_data, 1));
        all_data.Add(Math.Round(new_data, 1));
        textBox1.Text = Convert.ToString(Math.Round(new_data, 2));
        all_data.Sort();
        var peak_vacuum = all_data[0];
        textBox4.Text = peak_vacuum.ToString();

        if (sensorval.Count % 100 == 0)
        {
            sensorval.Sort();
            var find_max = sensorval[0];
            var find_min = sensorval[sensorval.Count - 1];
            textBox3.Text = find_min.ToString();
            textBox2.Text = find_max.ToString();
            sensorval.RemoveRange(0, 99);
        
        }

    }

我无法计算时差,因为 myDateTime 变量在 button2 中计算并在 button2 方法中定义。但是 DateTime.Now 是在计时器滴答方法中定义的。因此,我收到“名称 'myDateTime' 在当前内容中不存在”的错误。在计时器滴答方法中。 顺便说一下,我尝试在计时器滴答中使用计数器来查看程序运行后的秒数。它不是那么准确。它比实时慢。所以,我选择了上面的方法。 提前谢谢你。

我觉得是你想要的。

 public partial class Form1 : Form
 {
    // accessable from all methods, events on Form1
    private DateTime myDateTime;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        myDateTime = DateTime.Now;
        // other codes
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // other codes
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // other codes
        var diffrenceSpan = DateTime.Now - myDateTime;

        var hours = diffrenceSpan.Hours;
        var minutes = diffrenceSpan.Minutes;   
        var seconds = diffrenceSpan.Seconds;
    }
}

gits project links