没有返回经过的时间(以毫秒为单位)

时间:2017-08-29 19:36:30

标签: c# winforms

我已经构建了这个简单的秒表程序来测量时间,格式为:00:00:000 [分:秒:毫秒],但代码忽略了格式并按如下方式计数:00:00: [秒这里] [这里是毫秒],因此我只能得到10毫秒的经过时间而不是单个毫秒。

在这里显示:

enter image description here

实际经过的时间是3秒和610毫秒。

代码:

namespace stopwatch_1
{
    public partial class Form1 : Form
    {

        int timeMinutes, timeSeconds, timeMSeconds;
        bool timerActive;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            resetTime();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            timerActive = true;

        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timerActive = false;
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
           resetTime();
        }

        private void resetTime()
        {
            timerActive = false;
            timeMinutes = 0;
            timeSeconds = 0;
            timeMSeconds = 0;
        }

        private void timerStopwatch_Tick(object sender, EventArgs e)
        {
            if (timerActive == true)
            {
                timeMSeconds++;

                if (timeMSeconds >= 1000)
                {
                    timeMSeconds = 0;
                    timeSeconds++;

                    if (timeSeconds >= 60)
                    {
                        timeSeconds = 0;
                        timeMinutes++;

                    }

                }

            }


            timerDraw();

        }

        private void timerDraw()
        {
            labelMinutes.Text = String.Format("{0:00}", timeMinutes);
            labelSeconds.Text = String.Format("{0:00}", timeSeconds);
            labelMSeconds.Text = String.Format("{0:000}", timeMSeconds);

        }


    }
}`

计时器间隔设置为1,我已经双重检查所有变量是否指向正确的标签,所以我认为问题出在我将格式化要显示的字符串的位置,但是我&# 39;我不知道我哪里出错:

 private void timerDraw()
    {
        labelMinutes.Text = String.Format("{0:00}", timeMinutes);
        labelSeconds.Text = String.Format("{0:00}", timeSeconds);
        labelMSeconds.Text = String.Format("{0:000}", timeMSeconds);

    }

我不知道如何在这种情况下使用string.format,所以这可能是我出错的地方,所有的帮助都会受到赞赏

1 个答案:

答案 0 :(得分:3)

  

Windows窗体计时器组件是单线程的,的精度限制为55毫秒

您应该使用Stopwatch来获得更准确的解决方案:

Stopwatch stopwatch;

public Form1()
{
    InitializeComponent();

    stopwatch = new Stopwatch();
}

private void Form1_Load(object sender, EventArgs e)
{
    // do nothing
}

private void buttonStart_Click(object sender, EventArgs e)
{
    stopwatch.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
    stopwatch.Stop();
}
private void buttonReset_Click(object sender, EventArgs e)
{
    stopwatch.Reset();
}

private void timerStopwatch_Tick(object sender, EventArgs e)
{
    timerDraw();
}
private void timerDraw()
{
    labelMinutes.Text = String.Format("{0:00}", stopwatch.Elapsed.Minutes);
    labelSeconds.Text = String.Format("{0:00}", stopwatch.Elapsed.Seconds);
    labelMSeconds.Text = String.Format("{0:000}", stopwatch.Elapsed.Milliseconds);
}

编辑:

您还应该减少计时器上的Interval,因为refresh区间不再需要1ms个标签。