【问题标题】:Not returning elapsed time in milliseconds不以毫秒为单位返回经过的时间
【发布时间】:2017-08-30 04:00:03
【问题描述】:

我已经构建了这个简单的秒表程序来测量时间,格式为:00:00:000 [分钟:秒:毫秒],但代码忽略了格式并像这样计数:00:00:[秒此处][此处为毫秒],因此我只能以 10 毫秒为单位获得经过的时间,而不是单个毫秒。

这是显示:

实际经过的时间是 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,我仔细检查了所有变量是否都指向正确的标签,所以我认为问题在于我将字符串格式化显示在哪里,但我不确定在哪里我错了:

 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,所以这可能是我出错的地方,所有帮助将不胜感激

【问题讨论】:

  • 作为一项规则,请发布您的代码,而不是粘贴到您的代码的链接。这三个time* 变量是什么?
  • 首先,不要使用计时器来实际计算时间增量——它们对此不够可靠。相反,存储您启动计时器的时间 (DateTime start = DateTime.Now),然后每当您的计时器触发时,只需计算它已经持续了多长时间 (TimeSpan duration = DateTime.Now.Subtract(start))。
  • TimeSpan 结构具有 SecondsMilliseconds 等属性,因此找出需要打印出来的值很简单。
  • 具有讽刺意味的是,您发布的实际输出值的代码看起来不错!

标签: c# winforms


【解决方案1】:

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 上的标签。

【讨论】:

  • 这个!使用秒表 - 真不敢相信我忘记了那节课!
猜你喜欢
  • 2011-08-02
  • 2012-11-15
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2013-06-02
  • 1970-01-01
相关资源
最近更新 更多