views:

45

answers:

2

I have been trying to create a timer program with VB 2010 to the accuraccy of 0.05seconds (If possible, 0.01s)

I insert a timer into the form (Timer1, Interval - 50).

The code when the timer ticks:

    intdsecond = intdsecond + 5
    If intdsecond > 99 Then
        intdsecond = intdsecond - 100
        intsecond = intsecond + 1
    End If

    If intsecond > 59 Then
        intsecond = intsecond - 60
        intminute = intminute + 1
    End If

Note: intdsecond, intsecond and intminute are global variable used to record 0.01s, 1s and 1min time.

But when I run the timer for 1min, the recorded time was 48.05 sec

How can I make my timer more accurate? Is there anything i have done wrongly with the code?

Extra info: I am using windows 7, vb 2010, .Netframework 4 client profile.

A: 

You shouldn't write your timer logic expecting the interval to be completely precise. There are a variety of factors that can delay the timer (thread priority, latency in the code, etc.). And the shorter the interval, the greater the perceived error (and 50 milliseconds is quite short). Instead, you should always be comparing the current time with the start time (store the start time beforehand and compare against it) and use that for display purposes.

Kirk Woll
I know but i did not expect the error to be so great. Its almost 12sec in difference. Anyway, do you have any recommendation to make it more accurate (I would still like to make it 50ms). Thanks in advance.
yihang
@yihang, I thought I did recommend a solution to make it more accurate. :) You should store the start time in a variable/field, and **always** compare against it `DateTime.Now - startTime` when determining the elapsed time.
Kirk Woll
@Kirk, yes you did, but I did not make my question clear enough. I would want the time elapsed to to displayed on a label and it is refreshed frequently. So, I think your method of comparing starting time and ending time would not work for my case. Your method will be very useful if I do not need to have a display. Anyway, thanks for your help.
yihang
@yihang, I *specifically* expected you to display the time. Why do you think this solution will not work?
Kirk Woll
@Kirk, do you mean by comparing the starting time and the current time every, say, 5ms, and display the time elapsed? I did not think of it before...
yihang
A: 

If this is the System.Windows.Forms.Timer, it is not accurate to 50 ms:

The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace.

See the remarks on the documentation for System.Windows.Forms.Timer.

You might also consider System.Diagnostics.Stopwatch. It doesn't raise an event when the interval elapses, but if all you care about is the total elapsed time, it does provide some convenient properties (e.g. ElapsedMilliseconds).

adrift
That is what I want. Error for 1min is not even 0.5 s. Thanks. You solved my problem
yihang