解决  时间间隔问题

已加入
2020年10月13日
留言内容
3
编程经验
Beginner
你好,
我是C#的新手。我试图做一个秒表。我的问题是,我使用1 ms的间隔值时,无法将计时器与1/1000秒秒表匹配。
ms在10秒内达到1000,而必须在1s内达到。我该如何解决这个问题?
我的代码是:


2.png


Stopwatch:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Stopwatch
{
    public partial class Form1 : Form
    {
        int TimeCCs, TimeCs, TimeSec, TimeMin;
        bool IsActive;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            IsActive = false;

        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            IsActive = false;
            ResetTime();

        }

        private void ResetTime()
        {
            TimeCCs = 0;
            TimeCs = 0;
            TimeSec = 0;
            TimeMin = 0;
            //IsActive = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (IsActive)
            {
                timer1.Start();
                timer1.Interval = 1;
                TimeCCs++;
                if (TimeCCs >= 1000)
                {
                    TimeCs++;
                    TimeCCs = 0;
                }

                if (TimeCs >= 100)
                {
                    TimeSec++;
                    TimeCs = 0;
                    if (TimeSec >= 60)
                    {
                        TimeMin++;
                        TimeSec = 0;
                        
                    }
                }
            }
            DrawTime();

        }

        private void DrawTime()
        {
            LabelCCs.Text = string.Format("{0:000}", TimeCCs);
            LabelCs.Text = string.Format("{0:00}",TimeCs);
            LabelMin.Text = string.Format("{0:00}", TimeMin);
            LabelSec.Text = string.Format("{0:00}", TimeSec);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            IsActive = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ResetTime();
            IsActive = false;

        }
    }
}
 
Solution
作为 @约翰·H suggests, the Stopwatch class is what you use for actually measuring time. The Timer class is what you use to perform an action after a period of time. That action could be displaying the time you measured with a Stopwatch. The Timer will never be 100% accurate as far as when it raises its events so it can't be reliable for actual measurement of time. If you use a relatively small Interval though, the time displayed will always be accurate enough for the human eye. You probably wouldn't need anything less than 200 milliseconds. Just be sure to do one final refresh of the displayed time after stopping the Timer and...

约翰·H

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
1,051
地点
挪威
编程经验
10+
我建议您使用秒表类。它具有“开始”,“停止和重置”和“经过时间”属性的方法,该属性是可以按计时器间隔显示的TimeSpan。

文档说计时器的精度为55ms。
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,534
地点
悉尼,澳大利亚
编程经验
10+
作为 @约翰·H suggests, the Stopwatch class is what you use for actually measuring time. The Timer class is what you use to perform an action after a period of time. That action could be displaying the time you measured with a Stopwatch. The Timer will never be 100% accurate as far as when it raises its events so it can't be reliable for actual measurement of time. If you use a relatively small Interval though, the time displayed will always be accurate enough for the human eye. You probably wouldn't need anything less than 200 milliseconds. Just be sure to do one final refresh of the displayed time after stopping the TimerStopwatch to ensure that you are displaying the very last measured time and not a value that may have been measured shortly before stopping.
 
解决方案

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,534
地点
悉尼,澳大利亚
编程经验
10+
确定解决方案后,请使用第一篇文章上方的“已解决”按钮标记一个已解决主题。这节省了人们浪费时间打开已经解决的线程。这次我为您做了。
 
最佳 底部