calculate milliseconds with timer Tick in c#

8.4k views Asked by At

I want to display milliseconds in label with Timer_Tick event. But even if i initialize the Timer.interval to 1, I cant get the actual millisecond.

4

There are 4 answers

4
GvS On

Windows and C# are not created to be a realtime environment. A timer is not guaranteed to trigger on the exact millisecond that is set in the properties.

Aside from that, the typical LCD displays, nowadays run on 60 Hz. So you get a refresh interval that is larger as 1 millisecond. This means you cannot display milliseconds in a label on a display that has a refresh rate smaller as 1000 Hz.

0
Roman Starkov On

Had you better explained what you wanted to do in your question, I think you would have received better answers!

So you want to create a stopwatch.

The way to do this is to use DateTime.Now, which returns the current date and time. When the Start button is clicked, you save DateTime.Now for later use:

private DateTime startTime;

private void StartButton_Click(...)
{
    startTime = DateTime.Now;
}

Then, every now and then, you need to refresh the time shown in your label. You already have a Timer with an interval set to 1. This won't fire every millisecond; the interval is approximate. But this is not a problem!

private void Timer_Tick(...)
{
    TimeSpan timeElapsed = DateTime.Now - startTime;
    timeLabel.Text = timeElapsed.TotalSeconds.ToString("0.000");
}

So you don't rely on how often the event is fired. You simply calculate how much time has elapsed since you started the timer!

There are many ways you can display the resulting timeElapsed value. Check out the Hours, Minutes, Seconds and Milliseconds fields.

0
M4N On

If you want to implement a stop watch, then you might want to use the class System.Diagnostics.Stopwatch. Have a look at that page, it contains a sample program.

The Stopwatch class has methods Start(), Stop() and Reset() to start/stop/reset measurement. And you can use the Elapsed or ElapsedMilliseconds to get the time measured so far.

Regarding the resolution of the Stopwatch class, MSDN says this:

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

BTW: on my system the flag Stopwatch.IsHighResolution is set to true, and Stopwatch.Frequency returns a value of 3215342 (ticks per second) which is enough to measure microseconds.

0
josh On

As others posted, you do not get a ms-exact time when waiting for timer events in Windows. You may however, measure the elapsed time between timer events very exactly. The usual way of doing this is to use the QueryPerformanceCounter API.