Charles Kelly
Charles Kelly

Reputation: 29

Not returning elapsed time in milliseconds

I've built this simple stopwatch program to measure time in the format of: 00:00:000 [minutes:seconds:milliseconds], but the code ignores the format and counts up like this: 00:00:[seconds here][milliseconds here], so as a result I can only get the elapsed time in 10s of milliseconds and not the individual millisecond.

Here's the display:

enter image description here

The actual time elapsed is 3 seconds and 610 milliseconds.

Code:

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);

        }


    }
}`

The timer interval is set to one, and I've double checked that all variables are pointing to the right labels so I think the problem lies with where I've formatted the string to display, but I'm not sure where I've gone wrong:

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

    }

I don't really know how to use string.format in this context, so this is probably where I've gone wrong, all help would be appreciated

Upvotes: 0

Views: 1537

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds.

You should use a Stopwatch to get a more accurate resolution:

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);
}

EDIT:

You should also reduce the Interval on your timer, since there is no need to refresh the labels on a 1ms interval anymore.

Upvotes: 3

Related Questions