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.
If this is the
System.Windows.Forms.Timer, it is not accurate to 50 ms: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).