|
|
| |
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
|
You can generally minimise the amount of timers you use with the following code.
Option Explicit
Private Sub Form_Load()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static Second As Long
Static Minute As Long
'You only ever need one timer.
'Do what you want to do every millsecond
'
'With timer set to 1
If Second = 1000 Then
'Do what you want to do every second
Debug.Print "1 second has passed"
Second = 0
End If
If Minute = 10000 Then
Debug.Print "1 minute has passed"
'Do what you want to do every minute
Minute = 0
End If
Second = Second + 1
Minute = Minute + 1
Label1.Caption = Second & " " & Minute
End Sub
Sometimes a timers contents are mission critical and you do not want other codes getting mixed up in things and slowing everthing down.
So I opted for a priority system instead.
- 1st high priority timer.
- 2nd high priority timer.
- Low priority timer.
but otherwise I could have used the code to only ever use 1 timer.
Hope it helped, please add comments. | |
|
|
Other 26 submission(s) by this author
|
|
| |
Report Bad Submission |
|
| |
| Your Vote! |
|
See Voting Log |
| |
| Other User Comments |
3/9/2004 7:37:10 AM: Timothy Marin
'With timer set to 1 If Second = 1000 Then 'Do what you want to do every second
YOur Nuts if you think a vb timer fires 1000 times a second.. sure this is what you would think but it just isnt the case. a standard vb timer fires about 100* a second wich is why it sux for appz that need accurate timing like some games.
(If this comment was disrespectful, please report it.)
|
3/9/2004 8:28:11 AM: Ericos Georgiades
that's true, if you put 2 timers, on counting every 1000 (1 second) and another counting every 1, putting the second timer diplay the count of the other should show 1000 right? well no way near, unless you boost priority but still does'nt surpass 200 (If this comment was disrespectful, please report it.)
|
3/9/2004 8:29:27 AM: Ericos Georgiades
what i said sounds gibberish, basicaly i agree with reply #1 (If this comment was disrespectful, please report it.)
|
3/9/2004 2:16:34 PM: Luke H.
Theres a submission already that proves you only need one timer for your entire program, no matter how many forms or classes need timers. And an api timer instead of a VB one! Check out the submission 'Timers, simplified.' (If this comment was disrespectful, please report it.)
|
3/9/2004 2:24:50 PM: gurpreetsingh
Truly agreed actually VB timer fires only at 17 value in the interval. In simple words , Make it 1 or 17 NO difference. 17 and 18 will make some beacuse of the current hardware standards but even then 17 or 1 no difference. (If this comment was disrespectful, please report it.)
|
3/10/2004 5:05:05 PM: Jatopian
If you want to optimize your timing, use API... The timer control is horribly buggy. (If this comment was disrespectful, please report it.)
|
8/1/2004 12:46:16 PM:
by Shawn Cox (my name is invisible?) I had my VB Timer set to 1 interval and it was messing up, I set it to 2 and the code worked fine. The timer runs around 90-95 times a second for me. Personally I use SetTimer API (along with KillTimer). The only problems I have is if one of my programs is running while an emulator is, then the emulator runs like cr@p. (most likely because of my 333Mhz, 60MB of RAM) (If this comment was disrespectful, please report it.)
|
11/27/2009 5:22:23 AM: AOTChronoTrigger
The accuracy of the VB timer depends on the system being used, and is a combination of many factors, including (but not limited to) the resolution of the realtime clock, thread priority, thread state, and thread queue position. Same deal with DoEvents, which places thread execution into an idle/wait state, allowing the system to process messages posted to threads. Because VB applications are generally only "single-threaded", adding more timers will not increase your timer resolution, and can actually hurt performance. People tend to over-use timers, placing 10 or more on forms. This is terrible, sloppy practice. However, DO use more than one (and less than 4, for Heaven's sake) timer when the resolution is not critical (say, 500 to 5000ms), especially if it helps with code clarity/maintenance. (If this comment was disrespectful, please report it.)
|
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
To post feedback, first please login. |