-
Mar 3rd, 2010, 04:19 PM
#1
Classic VB - How can I have a Timer interval of more than a minute?
The maximum value of the Interval property for a Timer is 65535 milliseconds, which is only 65.5 seconds. If you want the code in your timer to run less often than that, you need to do something extra to allow it.
There are various methods you can use, some of which are shown below:
Use a counter variable
The simplest method is to set the Interval property to a certain value (such as 1000 for 1 second), and count the amount of times it occurs - and only run the rest of the code when the counter gets high enough:
Code:
In the "General"->"Declarations" section of the form (at the very top of the code file):
Private lngTimer as Long
In Form_Load:
lngTimer = 0
Timer1.Interval = 1000
Timer1.Enabled = True
At the start of the _Timer event:
'increase the counter
lngTimer = lngTimer + 1
'exit if it isn't high enough yet
If lngTimer <= 120 Then Exit Sub '120 seconds, which is 2 minutes
'reset so that the full delay will happen again
lngTimer = 0
The highest overall interval you can have now is 2147483648 times as big as the .Interval property. If you set .Interval to 1000 (1 second), the total is nearly 25 days!
This method is perfectly fine if you don't mind the overall interval being a bit inaccurate... for the example of 120 seconds, the actual time it takes might be 122 seconds. If your overall interval is 4 hours, the actual time taken might be 4 hours and 10 minutes.
Note that if you increase the value of the .Interval property, it will be even less accurate - but you should also be careful of making it too low (which will not make it much more accurate), because that will mean that it uses more CPU time.
Use a Date variable
With a minor change to the above method you can increase the accuracy to about 1 second (no matter how big your total interval is), by changing the counter variable to one that checks the date/time instead:
Code:
In the "General"->"Declarations" section of the form (at the very top of the code file):
Private datTimer as Date
In Form_Load:
datTimer = DateAdd("n", 5, Now) 'set it to fire after 5 minutes
Timer1.Interval = 500
Timer1.Enabled = True
At the start of the _Timer event:
'exit if it isn't time yet
If Now <= datTimer Then Exit Sub
'reset so that the full delay will happen again
datTimer = DateAdd("n", 5, Now)
The highest overall interval is now virtually unlimited.
...even more accurate versions
If you want to get more accurate than about 1 second, you will need something more complex than what is shown above. The accuracy of these can be as good as 1 millisecond.
The usual way to do that is use something more accurate than the current time, which could be the Timer function, the GetTickCount API, or something else. Each of those methods has it's own little quirks (for example, the Timer function resets to 0 at midnight), which require extra code to deal with it.
Another way is to use an API based timer, using the SetTimer API.
There are many examples of those methods on the forums, so with a Search you should be able to find a good one... and you can find a very good example of the API based option in VB6 SelfTimer class module
Last edited by si_the_geek; Mar 3rd, 2010 at 04:30 PM.
Tags for this Thread
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
|