All source code in Visual Basic Ask a Visual Basic Pro Discussion Forum Categories All jobs in Visual Basic
Timer,optimization
   Code/Articles � |  Newest/Best � |  Community � |  Jobs � |  Other � |  Goto � | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Visual Basic Stats

 Code: 5,136,215. lines
 Jobs: 279. postings

 How to support the site

 
Sponsored by:
Quick Search for:  in language:    
You are in:
 
Login





Latest postings for Visual Basic.
Improvised Employee Examination System
By kevern on 3/15


PortScanner
By Kaustubh Pratap Chand on 3/15


Click here to see a screenshot of this code!EXCEL SHEET LIKE PROTECCTION
By MUHAMMAD SAIF UL RAHMAN HASHMI on 3/15

(Screen Shot)

Click here to see a screenshot of this code!Risk for Stroke in Atrial Fibrillation
By Warren Goff on 3/4

(Screen Shot)

Click here to see a screenshot of this code!DFT - Discrete Fourier Transform of digitaly sampled data
By Dolac on 3/14

(Screen Shot)

ClickText *UPDATE*
By Lars Jansen on 3/14


Centroid in Polygon Finder for AutoCAD
By Mark Acuna on 3/4


Click here to see a screenshot of this code!Crypto
By Krishna Ram Prakash on 3/13

(Screen Shot)

LOCK YOUR TEXTBOX
By MUHAMMAD SAIF UL RAHMAN HASHMI on 3/13


Click here to see a screenshot of this code!SMSlogger
By Emilio Ficara on 3/8

(Screen Shot)

Click here to see a screenshot of this code!Real Photomosaic v12 (cieLAB)
By reexre on 3/12

(Screen Shot)

Click here to see a screenshot of this code!Kimmo - MyHelp
By Anele Mbanga on 9/24

(Screen Shot)

Click here to see a screenshot of this code!Kimmo - CoolCode Update
By Anele Mbanga on 2/9

(Screen Shot)

Click here to see a screenshot of this code!NIC VALIDATION
By Kasun Nayanajith Jayamuni on 3/12

(Screen Shot)

Click here to see a screenshot of this code!Bezier ART v.0
By reexre on 3/11

(Screen Shot)

Lista Mp3
By Massimiliano Ganzaborn on 3/17


Click here to see a screenshot of this code!Swarm of Gnats
By Paul Bahlawan on 7/15

(Screen Shot)

Click here to see a screenshot of this code!Create runtime popup menu
By Red Corn on 3/11

(Screen Shot)

Click here to see a screenshot of this code!Time Engine 7.7.661
By Kaveh Abdollahi on 3/10

(Screen Shot)

Click here to see a screenshot of this code!DM Timepicker
By Frank Donckers on 3/10

(Screen Shot)

Click here to see a screenshot of this code!Time Engine 7.7.661
By Kaveh Abdollahi on 3/9

(Screen Shot)

Click here to see a screenshot of this code!RGB <--> Cie Lab Converter
By reexre on 3/9

(Screen Shot)

Click here to see a screenshot of this code!DmDatePicker
By Frank Donckers on 3/3

(Screen Shot)

Click here to see a screenshot of this code!Air Line Reservation History System
By Muhammad Faraz Shaikh on 3/6

(Screen Shot)

Sales and Inventory
By Eduard Dueñas on 3/8


Enrollment System
By Eduard Dueñas on 3/8


Click here to see a screenshot of this code!School Management System
By Ashar Babar on 3/8

(Screen Shot)

Phonebook
By Andron Smith on 3/7


Click here to see a screenshot of this code!Formats the time in Real-Time.
By programadorvb6 on 3/7

(Screen Shot)

sticky VB windows that stick to each other (like seen in Winamp)
By Louis Coder on 1/9


Click here to see a screenshot of this code!calculator
By wadii el jaziri on 3/6

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

You only ever need 1 timer

Print
Email
 
article
Submitted on: 11/16/2009 9:02:44 AM
By: TonyGG  
Level: Intermediate
User Rating: By 1 Users
Compatibility:VB 6.0

Users have accessed this article 4724 times.
 
author picture
 
     Timer optimization.

 
 
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.

  1. 1st high priority timer.
  2. 2nd high priority timer.
  3. 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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
3/9/2004 7:37:10 AMTimothy 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 AMEricos 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 AMEricos 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 PMLuke 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 PMgurpreetsingh

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 PMJatopian

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 AMAOTChronoTrigger

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.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Games | Feedback | Customize | Visual Basic Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright� 1997-2010 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.