|
-
Jun 26th, 2000, 10:28 AM
#1
Thread Starter
New Member
I Know that the timer interval only allows you to go as far as 1min. and 5 seconds.....so how would i go about setting the timer to go on for about a hour? How would i code the timer?
-
Jun 26th, 2000, 10:36 AM
#2
Fanatic Member
You can't!
You can either use two timers (ugly) or one time with a counter variable, which increments.
eg, set timer interval to 1 minute
every timed event increment MyInt
then execute every 60 minutes with this
Code:
UpdateMins = 60 ' Constant in module
' chuck this in the timer event
If MyInt Mod UpdateMins = 0 then
' Execute code
End if
Have a good one
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 26th, 2000, 10:58 AM
#3
Hyperactive Member
The answer is simple. Use the "Tag" property to store the time you want the timer to go off. Then do a test to determine if it has elapsed or not
Code:
tmTimer.Interval = 1000
tmTimer.Tag = dateadd("s",120,now())
tmTimer.Enabled
Public Sub tmTimer_Interval()
If datediff("s",now(),datevalue(tmTimer.Tag)) <= 0 Then
tmTimer.Tag = dateadd("s",120,now())
' Do your code here
End if
End Sub
I haven't tested the code but thats the general gist
-
Jun 26th, 2000, 11:02 AM
#4
Fanatic Member
take a look at this web site. Fox uses API to create a new timer
-
Jun 26th, 2000, 01:44 PM
#5
Frenzied Member
What about the timer function?
Some people don't know it, but instead of using the timer control, you can often get by with using the timer function. It is a function built into VB that returns the seconds since midnight.
I use it all the time for a Wait() function:
Code:
Private Sub Wait(asSeconds As Single)
Dim lsTimesUp As Single
lsTimesUp = Timer + asSeconds
Do Until Timer >= lsTimesUp
DoEvents
Loop
End Sub
Putting DoEvents in the loop allows other processes to function. The only problem is when you get close to midnight, adding the wait time to the timer may make the number bigger than the max Timer value (the Timer resets to 0 at midnight). To handle this, I added the following code:
Code:
Private Sub Wait(asSeconds As Single)
Dim lsTimesUp As Single
Dim liDaysAway As Integer
Dim ldDayHasArrived As Date
lsTimesUp = Timer + asSeconds
' This loop subtracts one day's worth of seconds
' for each day that "TimesUp" goes past midnight
' and keeps track of how many days were counted
Do While lsTimesUp > 86399.99
liDaysAway = liDaysAway + 1
lsTimesUp = lsTimesUp - 86399.99
Loop
' If TimesUp extends into tomorrow (or beyond),
' wait the appropriate number of days
If liDaysAway > 0 Then
ldDayHasArrived = Date + liDaysAway
Do Until Date = ldDayHasArrived
DoEvents
Loop
End If
' Now that the correct day has arrived, wait
' until the appropriate second
Do Until Timer >= lsTimesUp
DoEvents
Loop
End Sub
Note: On my machine, the Timer maxes out at 86399.99 then resets to 0 at midnight. Your machine may vary. The code above has not been throughly tested, it was re-written from memory since I don't have my original function with me.
Usage for this function:
Code:
Private Sub Command1_Click()
Dim lsSeconds As Single
lsSeconds = InputBox("Enter the number of seconds you want to wait")
Wait lsSeconds
MsgBox "Time's Up!!!"
End Sub
[Edited by seaweed on 06-27-2000 at 02:48 AM]
-
Jun 26th, 2000, 01:57 PM
#6
Hyperactive Member
a while ago a knocked a dll to get round this problem.
It inbuilt intervals of 30 secs, 1 min, 10 min,, 30 min, 1 hr, 6 hr, or 1 day. Or you can specify your own time period (in mutliples of any of the above).
If you want it give me a shout.
"One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig
[email protected]
"but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.
-
Jun 26th, 2000, 02:12 PM
#7
Frenzied Member
It works
I just tested my function above, and it works just fine. You can run into the next day and the timer still goes off. You can also do other events within your own app and other applications (just don't unload your app).
I think it would probably be a good idea to create a global flag that would act as another test for the Do Loop, so that if your user shuts down your app before the timer expires, you end the loop.
example:
Code:
Option Explicit
Private mbFlag As Boolean
Private Sub Form_Load()
mbFlag = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
mbFlag = True
End Sub
Private Sub Wait(asSeconds As Single)
' .....
' The code is the same until you get to the first loop:
Do Until Date = ldDayHasArrived Or mbFlag = True
DoEvents
Loop
' .....
' And again in the second loop:
Do Until Timer >= lsTimesUp Or mbFlag = True
DoEvents
Loop
End Sub
-
Jun 26th, 2000, 02:26 PM
#8
PowerPoster
Full Code Timer
You may need to use the code timer SetTimer and KillTimer API function.
Code:
'Code under Basic Module File
Option Explicit
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
MsgBox "Time UP!"
End Sub
Private Sub Form_load()
'1 second = 1000ms
'1 min 5 sec = 65000ms
"Create a Code Timer
SetTimer Me.hwnd, 0, 65000, AddressOf TimerProc
End Sub
Private Subn Form_QueryUnload()
'Kill the Code Timer
KillTimer Me.hwnd, 0
End Sub
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
|