|
-
May 9th, 2005, 02:26 AM
#1
Do not use Timers incorrectly
Many people are having problems with timers and asking about timers. If you want to use a timer in your app. Please follow these suggestion to help insure that you do not have problems.
1) In VB6, Do not Disable and Re-Enable your timer. This will cause your timer to still run after the program is terminated causing the timer function to still run (unless you disable the timer in your unload code).
Upon entering the timer code use
VB Code:
Private Sub Timer1_Timer()
'Add your code here
End Sub
2) You can use more than one timer on a form.
Seperate your code into each timer that will interrupt on it own interval. Timers should not be confused with timing functions. A timer will interrupt your code on the set intervals so that you may do other things. Timing functions are called to time the elapsed time between set areas in code or time betwwen events happening.
Following these rules will make sure that your problems are not timer issues.
Last edited by randem; May 9th, 2005 at 04:13 PM.
-
May 9th, 2005, 02:37 AM
#2
Re: Do not use Timers incorrectly
Nice thing randem!
I'll like to add
3) Check whether you need a timer at all or not
Timers run in seperate thread. Your code continues to run after enabling the timer. Usually u want to just wait for the timer to finish and then resume your code. In such cases just use loops with DoEvents instead of timers.
-
May 9th, 2005, 02:49 AM
#3
Re: Do not use Timers incorrectly
You could even go as far as saying, don't use Timers at all. Using GetTickCount is better, and QueryPerformanceCounter is even better. Try for those 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 9th, 2005, 02:59 AM
#4
Re: Do not use Timers incorrectly
chemicalNova,
Timers are essential. You can't interrupt running code with GetTickCount. That is the main purpose of timers... To interrupt running code.
You are confusing Timers with Timing.
Last edited by randem; May 9th, 2005 at 04:19 AM.
-
May 9th, 2005, 06:56 AM
#5
Hyperactive Member
Re: Do not use Timers incorrectly
Nice post. Very helpful.
-
May 9th, 2005, 08:51 AM
#6
Re: Do not use Timers incorrectly
 Originally Posted by randem
VB Code:
Private Sub Timer1_Timer()
Timer1.Enabled = False
'Add your code here
Select Case tmrflg
Case (First Condition)
Case (Second Condition)
....
End Select
'and just before exiting the timer code do
Timer1.Enabled = True
End Sub
Shouldn't it be?:
Nobody knows what software they want until after you've delivered what they originally asked for.
Don't solve problems which don't exist.
"If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)
2 idiots don't make a genius.
-
May 9th, 2005, 10:00 AM
#7
Re: Do not use Timers incorrectly
I really don't understand what's the fuss about timers...
I have an application with about 30 forms, some of the forms have multiple instances while app is running, and ALL forms have at least one timer on each form... have not had ANY problem with the app as of yet regarding timers. Memory is fine, CPU usage is low, I really don't understand why you guys have a problem with timers.
And by the way, this:
VB Code:
Private Sub Timer1_Timer()
Timer1.Enabled = False
'Add your code here
'and just before exiting the timer code do
Timer1.Enabled = True
End Sub
NEVER HAPENS to interrupt itself !! Did you actually try it ?
A timer never fires again until you exit the current instance... so what's the point of that code ?
-
May 9th, 2005, 11:39 AM
#8
Re: Do not use Timers incorrectly
You should never be using Timers to begin with unless you are that desperate on low CPU usage. Timers are slow, inaccurate, and inconsistant. It's even worse on XP computers since they go 10 times faster, making them really inaccurate and inconsistant. And you are right. It's even more worse when having two or more timers run at the same time. Using managed loops with a frame limiter is the way to go for better accuracy. And that loop should be the only loop in your app that'll take care of everything, such as a Game Loop. But that would mean more CPU usage.
Last edited by Jacob Roman; May 9th, 2005 at 11:42 AM.
-
May 9th, 2005, 12:40 PM
#9
Re: Do not use Timers incorrectly
Anyone wants to prove me wrong with actual proof (code) ??
I know the timer is inacurate, so prove me wrong on other things that you think it's bad about timers...
I'm curious on why everybody is so disapointed about the timer control, cuz I never had any problems with it in my 8 years of programming in VB.
-
May 9th, 2005, 02:30 PM
#10
Re: Do not use Timers incorrectly
First of all, a timer does NOT interrupt (this is not C++ to use interrupts), and the following code proves it !
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private LastTick As Long
Private Sub Form_Load()
LastTick = GetTickCount
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
Debug.Print "Timer1_Timer: " & GetTickCount - LastTick
LastTick = GetTickCount
Pause 1 ' pause for one second
End Sub
Private Sub Pause(ByVal Interval As Single)
Dim EndTime As Single
EndTime = Timer + Interval
Do
DoEvents
Loop Until Timer >= EndTime
End Sub
Output:
Timer1_Timer: 500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
keeps going like this...
What does this prove ?
First of all, if a timer would really Interrupt, then you would get an event EVERY 500 ms, and guess what ? it does not...
This proves that the interval you set in a timer is the time it takes from the time the timer sub ends to the time the sub get's called again. The time spent inside the call is added to the total time (timer's Interval), THEREFORE everyone thinks that a timer is not precise !! and guess what, if you add the 500 ms with the 1 second you will have a total of 1500 ms, from what I see the timer interval takes exactly 1500... hmmm what a coincidence !
It seems that a timer is more precise than I originaly thought...
-
May 9th, 2005, 03:59 PM
#11
Re: Do not use Timers incorrectly
CVMichael,
You are indeed correct. MS must have changed the timers.
-
May 9th, 2005, 04:13 PM
#12
Re: Do not use Timers incorrectly
Thank you... I feel much better now 
Any other bad things about timers ?
I can't wait until this "myth" of "timers = bad" ends.
-
May 9th, 2005, 04:48 PM
#13
Re: Do not use Timers incorrectly
That's weird , I thought there was another post between post number 9 and 10... hmmm....
-
May 9th, 2005, 08:13 PM
#14
Re: Do not use Timers incorrectly
When using Timers in XP, they are 10 times faster than usual. And GetTickCount is not as accurate. It's only 10 ms accurate. Best to use the QueryPerformanceCounter And the QuerePerformanceFrequency API's for 1 ms accuracy.
-
May 9th, 2005, 11:22 PM
#15
Re: Do not use Timers incorrectly
Hmmm, same code as above, but on my home computer, I get this output:
Timer1_Timer: 501
Timer1_Timer: 1502
Timer1_Timer: 1502
Timer1_Timer: 1502
Timer1_Timer: 1503
Timer1_Timer: 1502
It seems that a timer is more acurate on some comptuers than others...
-
May 10th, 2005, 02:19 PM
#16
Re: Do not use Timers incorrectly
I'm just curious what output do you guys get when you run the following function:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
FindTimeAccuracy
End Sub
Private Sub FindTimeAccuracy()
Dim SmallestDiff As Single, PrevTime As Single, CurrTime As Single, K As Long
Dim CurrTick As Long, PrevTick As Long
PrevTime = Timer
SmallestDiff = 2 ^ 16
For K = 0 To 1000000
CurrTime = Timer
If PrevTime <> CurrTime Then
If CurrTime - PrevTime < SmallestDiff Then SmallestDiff = CurrTime - PrevTime
PrevTime = Timer
End If
Next K
Debug.Print "Timer = " & SmallestDiff * 1000 & " ms"
PrevTick = GetTickCount
SmallestDiff = 2 ^ 16
For K = 0 To 10000000
CurrTick = GetTickCount
If PrevTick <> CurrTick Then
If CurrTick - PrevTick < SmallestDiff Then SmallestDiff = CurrTick - PrevTick
PrevTick = GetTickCount
End If
Next K
Debug.Print "GetTickCount = " & SmallestDiff & " ms"
End Sub
I get this output:
Timer = 15.625 ms
GetTickCount = 15 ms
What do you get ?
-
May 24th, 2005, 03:54 PM
#17
New Member
Re: Do not use Timers incorrectly
 Originally Posted by CVMichael
First of all, a timer does NOT interrupt (this is not C++ to use interrupts), and the following code proves it !
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private LastTick As Long
Private Sub Form_Load()
LastTick = GetTickCount
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
Debug.Print "Timer1_Timer: " & GetTickCount - LastTick
LastTick = GetTickCount
Pause 1 ' pause for one second
End Sub
Private Sub Pause(ByVal Interval As Single)
Dim EndTime As Single
EndTime = Timer + Interval
Do
DoEvents
Loop Until Timer >= EndTime
End Sub
Output:
Timer1_Timer: 500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
Timer1_Timer: 1500
keeps going like this...
What does this prove ?
First of all, if a timer would really Interrupt, then you would get an event EVERY 500 ms, and guess what ? it does not...
This proves that the interval you set in a timer is the time it takes from the time the timer sub ends to the time the sub get's called again. The time spent inside the call is added to the total time (timer's Interval), THEREFORE everyone thinks that a timer is not precise !! and guess what, if you add the 500 ms with the 1 second you will have a total of 1500 ms, from what I see the timer interval takes exactly 1500... hmmm what a coincidence !
It seems that a timer is more precise than I originaly thought...
Yes you are right! BUT!!! Try bigger or lower values for timer interval. For example - if 10 then relust is:
Timer1_Timer: 16
Timer1_Timer: 1016
Timer1_Timer: 1015
Timer1_Timer: 1016
Timer1_Timer: 1015
and go on...
My CPU is AMD 3000 + with 512 MB RAM
-
May 24th, 2005, 04:45 PM
#18
Re: Do not use Timers incorrectly
I already knew that the timer is not precise. The point I was trying to make is that the timer does not Interrupt. Randem deleted the post where he was stating that timers interrupt... now I look like an fool because of that... He also edited his first post. Now this thread is kinda poitless because there is no good proof for "Do not use Timers incorrectly".
Edit: Edited post language - Hack
Last edited by Hack; May 24th, 2005 at 06:40 PM.
-
May 28th, 2005, 05:28 AM
#19
Re: Do not use Timers incorrectly
 Originally Posted by CVMichael
I'm just curious what output do you guys get when you run the following function:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
FindTimeAccuracy
End Sub
Private Sub FindTimeAccuracy()
Dim SmallestDiff As Single, PrevTime As Single, CurrTime As Single, K As Long
Dim CurrTick As Long, PrevTick As Long
PrevTime = Timer
SmallestDiff = 2 ^ 16
For K = 0 To 1000000
CurrTime = Timer
If PrevTime <> CurrTime Then
If CurrTime - PrevTime < SmallestDiff Then SmallestDiff = CurrTime - PrevTime
PrevTime = Timer
End If
Next K
Debug.Print "Timer = " & SmallestDiff * 1000 & " ms"
PrevTick = GetTickCount
SmallestDiff = 2 ^ 16
For K = 0 To 10000000
CurrTick = GetTickCount
If PrevTick <> CurrTick Then
If CurrTick - PrevTick < SmallestDiff Then SmallestDiff = CurrTick - PrevTick
PrevTick = GetTickCount
End If
Next K
Debug.Print "GetTickCount = " & SmallestDiff & " ms"
End Sub
I get this output:
Timer = 15.625 ms
GetTickCount = 15 ms
What do you get ?
I also get
Timer = 15.625 ms
GetTickCount = 15 ms
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|