Resolved Time interval problem

Alex2020

New member
Joined
Oct 13, 2020
Messages
3
Programming Experience
Beginner
Hi,
I'm new to C#. I tried to make a stopwatch. My problem is that I cannot match the timer with 1/1000 s stopwatch, while I have used interval value of 1 ms.
The ms reaches to 1000 in 10s while it must reach in 1s. How can I solve this issue?
My code is:


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
As @JohnH 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 Stopwatch to ensure that you are displaying the very last...
I recommend you use the Stopwatch class. It has methods to Start, Stop and Reset, and Elapsed property that is a TimeSpan you can show at the timer intervals.

Timer has a 55ms precision says documentation.
 
As @JohnH 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 Stopwatch to ensure that you are displaying the very last measured time and not a value that may have been measured shortly before stopping.
 
Solution
Please use the Resolved button above your first post to mark a thread Resolved once you have a resolution. This saves people wasting time opening threads that have already been resolved. I have done it for you on this occasion.
 
Back
Top Bottom