|
-
May 8th, 2003, 02:43 PM
#1
Delay timer * resolved
Can someone give me an overview on using SetTimer (and probably KillTimer also) in somewhat layman terms?
I'm planning to execute/call it in the module code. Is this possible or should it be in the form code? And when it's running, how do I go about executing code when it expires?
Last edited by leinad31; May 8th, 2003 at 05:31 PM.
-
May 8th, 2003, 03:04 PM
#2
VB Code:
Private Declare Function SetTimer _
Lib "user32" ( _
ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Ok, that's the declaration part. The hWnd is the hWnd associated by the timer, pass in the hWnd of the form.
nIDEvent is a unique identifier, you can pass any number > 0 to this. If you need more then one timer use different values here.
uElapse is the Interval.
lpTimerFunc, pass the address of the callback function to this argument: AddressOf TimerProc
The TimerProc must exist as a public sub in a module and it looks like this:
VB Code:
Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
'your code
End Sub
The arguments passed to the TimerProc are:
hWnd = the hWnd you passed to SetTimer.
uMsg = WM_TIMER message (always, so you can ignore this)
idEvent = the unique nIDEvent number you passed to SetTimer check this value if you have several timers enabled
dwTime = the number of milliseconds that has elapsed since the system was started. This is the same value as the return value of GetTickCount.
Before your application ends make sure you call KillTimer otherwise you'll get a GPF error. KillTimer takes the hWnd and the nIDEvent values you passed to SetTimer.
-
May 8th, 2003, 03:21 PM
#3
Oh, so it doesn't expire....
So I pass the hWnd of the main form (mine can either be frmServer or frmClient) even though I'll call it from a module (say modDgramProcessing)?
Last edited by leinad31; May 8th, 2003 at 03:26 PM.
-
May 8th, 2003, 03:30 PM
#4
And another thing, will TimerProc() apply to all timers created with SetTimer? I was hoping to create two groups of timers.
-
May 8th, 2003, 03:39 PM
#5
You can have more than one TimerProc (it doesn't have to be named TimerProc you can call it whatever you like). However you can, as I explained, use the same timer procedure, just add code to check the unique event value.
-
May 8th, 2003, 03:52 PM
#6
Ok thanks. I read about it again. Sorry I did not understand what you meant immediately.
Last edited by leinad31; May 8th, 2003 at 04:12 PM.
-
May 8th, 2003, 04:37 PM
#7
Alright I think I get the hang of it. Anyway, what I'm planning is a delay timer for my winsocks. Do you think using the socket index as nIDEvent is a good idea?
I was thinking of doing that since sock.Index is unique and so I won't have problems referencing the appropriate socket once TimerProc() fires (& vice versa with socket unload).
Last edited by leinad31; May 8th, 2003 at 04:41 PM.
-
May 8th, 2003, 04:44 PM
#8
-
May 8th, 2003, 04:48 PM
#9
Using the control index sounds like a good idea. However after reading your last post I became a little bit confused... I hope you've understand that after you have called SetTimer your TimerProc procedure will be called time after time... with a pause of dwTime number of milliseconds between the calls? In other words it works much like the VB timer except that you can use a long value for the interval (dwTime) instead of an Integer.
-
May 8th, 2003, 04:50 PM
#10
Originally posted by leinad31
Can't hWnd be Null?
Yes.
-
May 8th, 2003, 05:12 PM
#11
I realized the same thing after smoking a cigarette. Seems I need to store the delay duration (random interval). I tried tinkering with a collection (first time I tried) but I can refer to the item only through the index. I have to be able to refer to it by the key (socket index/nIDEvent) so I can compare if n milliseconds has passed. I probably can iterate through the collection but it'll slow things down.
Any idea how I can do this?
About hWnd being Null, just curious since I couldn't pass Null to SetTimer. But its ok, I have a form.hWnd that I can use.
Last edited by leinad31; May 8th, 2003 at 05:16 PM.
-
May 8th, 2003, 05:21 PM
#12
You can pass 0 as the hWnd argument...
You can still use SetTimer for what you need todo, just call KillTimer in the TimerProc.
-
May 8th, 2003, 05:28 PM
#13
Your right, they don't have to be of the same interval. Stupid me. I'm making things more complicated than it should be. )
Thanks for all the help. I really appreciate it.
Last edited by leinad31; May 8th, 2003 at 05:33 PM.
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
|