|
-
August 2nd, 2004, 12:36 PM
#1
Need a timer, what is the preferred method
In a vb5 application ( I can port to VB6 if necessary) I need to run some code every 30 minutes, possibly more frequently. What is my best option for a timer? Obviously I don't want something that will tax system resources all day long.
I should probably elaborate that the application should not be suspended while the timer is running.
Appreciate your help.
Last edited by Mothra; August 2nd, 2004 at 12:57 PM.
-
August 2nd, 2004, 02:07 PM
#2
Do you need it to be at the top and bottom of the hour, or just 30 minutes apart? The following code may not work well if you need to run the code at a certain time.
Put a Timer on your Form and set the Interval (the lower you set it, the closer to exactly 30 minutes your code will run).
Code:
Private Sub Timer1_Timer()
Static s_dtmNextRunTime As Date
'Stop Timer while executing code
Timer1.Enabled = False
If Now > s_dtmNextRunTime Then
'Code to execute every 30 minutes
'Reset s_dtmNextRunTime
s_dtmNextRunTime = DateAdd("m", 30, Now)
End If
'Restart Timer
Timer1.Enabled = True
End Sub
I'd rather be wakeboarding...
-
August 2nd, 2004, 03:35 PM
#3
Malleyo, Thanks for your reply. The time does not matter as long as intervals are 30 minutes.
I'm not sure I'll have a form for this, but I will look into your example none the less. I'm aware of the timer control I have never had the need for it though. I always assumed it used quite a bit of resources compared to other methods (maybe a bad assumption). I have also found some information on using the API SetTimer and KillTimer. I'm just shooting for the least overhead since the timer will be running continuously. o it does not need to fire at a specific time, just 30 min. intervals.
Probably this afternoon I'll compare all the methods that I know of and determine which will work the best...
-
August 2nd, 2004, 10:23 PM
#4
The best will be SetTimer() API even if it force you to add a form, which in this case will only be used to received the event. The problem with the VB Timer control is that you cannot set it to big value, while you can make it work like proposed in the previous post, it would be better to use API imo.
JeffB
-
August 3rd, 2004, 03:11 AM
#5
 Originally Posted by JeffB
The best will be SetTimer() API even if it force you to add a form,
JeffB
I may be wrong, but it seemed to me I was succesfully using the Api Timer without
a form (used hwnd of desktop...or even 0...and got the id of timer as obtained by
SetTimer api to KillTimer it later)
Last edited by Cimperiali; August 4th, 2004 at 05:43 AM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
August 3rd, 2004, 01:27 PM
#6
Just looked in MSDN, it seems you can use a TimerProc function instead of a form handle, so you are right Cesare 
JeffB
-
August 3rd, 2004, 04:35 PM
#7
You could always set it up as a scheduled task.
Terminate the app when you are done doing whatever. The task scheduler will start it every 30 minutes.
LJ
Hope is the feeling that you get that the feeling that you have will not last very long. 
-
August 4th, 2004, 02:19 PM
#8
On second thought Cesare, if you don't have a form, you'll need to use a LOOP with DoEvents, and I think the preferred method would be to not use DoEvents considering it take lot of CPU usage*. For an application that only need to run each 30 minutes, it should not be always running like that imo.
JeffB
-
August 7th, 2004, 05:52 PM
#9
Yes, JeffB, I would avoid a do...loop: better to have a form!
To tell the truth, it happened to me to have a form but also a timerApi
in bas that did not used form hwnd...do not remember exactly why...
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
August 9th, 2004, 08:34 PM
#10
but, is the thing with timers and forms as targets merely that the api needs the address of a callback function.. everytime the timer pulses, the cpu runs the code found at that address? in which case, write your function, and then set the timer with:
... AddressOf MySubRoutine
?
or is it that you must provide a target consumer of WM_TIMER messages? i cant remember.. maybe it's too late.. night all
-
August 10th, 2004, 04:47 AM
#11
Hallo Mothra,
Look at the "CallBack Function" within the "CallBack Function.rar " from here.
james
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
|