Results 1 to 13 of 13

Thread: Delay timer * resolved

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    VB Code:
    1. Private Declare Function SetTimer _
    2.  Lib "user32" ( _
    3.  ByVal hWnd As Long, _
    4.  ByVal nIDEvent As Long, _
    5.  ByVal uElapse As Long, _
    6.  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:
    1. Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    2.     'your code
    3. 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.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    And another thing, will TimerProc() apply to all timers created with SetTimer? I was hoping to create two groups of timers.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Can't hWnd be Null?

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by leinad31
    Can't hWnd be Null?
    Yes.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can pass 0 as the hWnd argument...

    You can still use SetTimer for what you need todo, just call KillTimer in the TimerProc.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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
  •  



Click Here to Expand Forum to Full Width