Joris wrote:Can anyone explain a bit more on how to use it?
What differs from the old API (which still can be used)?
What is that @thandle.i or how to get it, for example in the CreateTimerQueueTimer()?
What are the (important) benefits against the API's like timeSetEvent?
Has anyone more sample code or some replacement for the Timer_Include.pbi on the link here?
Hello Joris. Besides the fact that Microsoft has earmarked the timeSetEvent() function as obsolete, I personally find the two functions quite similar. They both have the same basic functionality, although they each have their own set of flags that determine threading, execution, response, etc. So, unless you have some very specific requirements on such behaviour, it shouldn't make much of a difference which one you use. Now, how to use it.
These first two examples demonstrate the simplicity of implementing these functions, by displaying the current time at one-second intervals, for ten seconds.
timeSetEvent example:
Code: Select all
Procedure timerProc(hTimer.i, uMsg.i, idTimer.i, wParam.i, lParam.i)
Debug FormatDate("%hh:%ii:%ss", Date())
EndProcedure
timer = timeSetEvent_(1000, 0, @timerProc(), 1, #TIME_PERIODIC)
quitTimer = ElapsedMilliseconds()
While ElapsedMilliseconds() - quitTimer < 10000 : Wend
KillTimer_(0, timer)
CreateTimerQueue example:Code: Select all
Import "kernel32.lib"
CreateTimerQueue()
CreateTimerQueueTimer(hTimer, hTimerQueue, timerCallback,
param, dueTime, period, flags)
DeleteTimerQueueEx(hTimerQueue, completionEvent)
EndImport
Procedure timerProc(param.i, timer.i)
Debug FormatDate("%hh:%ii:%ss", Date())
EndProcedure
timerQueue = CreateTimerQueue()
CreateTimerQueueTimer(@timer, timerQueue, @timerProc(), 0, 0, 1000, 0)
quitTimer = ElapsedMilliseconds()
While ElapsedMilliseconds() - quitTimer < 10000 : Wend
DeleteTimerQueueEx(timerQueue, 0)
In response to your question, about the
@thandle.i parameter in the CreateTimerQueueTimer() function;
thandle is a variable
(I just named it "timer" in my example) that will be assigned a unique handle to the timer that is being created. Since this function is capable of creating multiple timers, this handle would be required to delete it without deleting the entire queue. This is illustrated in this next example, where three different timer functions are running concurrently
(SetTimer(), timeSetEvent(), CreateTimerQueueTimer()), each with two independent active timers, set at different intervals. They each display the current time, at one-second and five-second intervals respectively, and can be terminated individually by clicking on their corresponding buttons:
Code: Select all
Global timer_ST, timer_SE
Import "kernel32.lib"
CreateTimerQueue()
DeleteTimerQueueTimer(hTimerQueue, hTimer, completionEvent)
DeleteTimerQueueEx(hTimerQueue, completionEvent)
CreateTimerQueueTimer(hTimer, hTimerQueue, timerCallback,
param, dueTime, period, flags)
EndImport
Procedure timerProc_ST(hWnd.i, uMsg.i, idTimer.i, dwTime.i)
If idTimer = timer_ST
SetGadgetText(1, "SetTimer 1: " + FormatDate("%hh:%ii:%ss", Date()))
Else
SetGadgetText(2, "SetTimer 2: " + FormatDate("%hh:%ii:%ss", Date()))
EndIf
EndProcedure
Procedure timerProc_SE(hTimer.i, uMsg.i, idTimer.i, wParam.i, lParam.i)
If hTimer = timer_SE
SetGadgetText(3, "timerSetEvent 1: " + FormatDate("%hh:%ii:%ss", Date()))
Else
SetGadgetText(4, "timerSetEvent 2: " + FormatDate("%hh:%ii:%ss", Date()))
EndIf
EndProcedure
Procedure timerProc_Q(param.i, timer.i)
SetGadgetText(5, "CreateTimerQueue 1: " + FormatDate("%hh:%ii:%ss", Date()))
EndProcedure
Procedure timerProc_Q2(param.i, timer.i)
SetGadgetText(6, "CreateTimerQueue 2: " + FormatDate("%hh:%ii:%ss", Date()))
EndProcedure
wFlags = #PB_Window_ScreenCentered | #PB_Window_SystemMenu
OpenWindow(0, #PB_Any, #PB_Any, 600, 100, "Windows Timer Functions", wFlags)
TextGadget(1, 50, 20, 140, 30, "SetTimer 1: ")
TextGadget(2, 50, 50, 140, 30, "SetTimer 2: ")
TextGadget(3, 220, 20, 150, 30, "timerSetEvent 1: ")
TextGadget(4, 220, 50, 150, 30, "timerSetEvent 2: ")
TextGadget(5, 410, 20, 150, 30, "CreateTimerQueue 1: ")
TextGadget(6, 410, 50, 150, 30, "CreateTimerQueue 2: ")
ButtonGadget(7, 25, 15, 20, 20, "X")
ButtonGadget(8, 25, 47, 20, 20, "X")
ButtonGadget(9, 195, 15, 20, 20, "X")
ButtonGadget(10, 195, 47, 20, 20, "X")
ButtonGadget(11, 385, 15, 20, 20, "X")
ButtonGadget(12, 385, 47, 20, 20, "X")
timer_ST = SetTimer_(0, 0, 1000, @timerProc_ST())
timer_ST2 = SetTimer_(0, 0, 5000, @timerProc_ST())
timer_SE = timeSetEvent_(1000, 0, @timerProc_SE(), 1, #TIME_PERIODIC)
timer_SE2 = timeSetEvent_(5000, 0, @timerProc_SE(), 1, #TIME_PERIODIC)
timerQueue = CreateTimerQueue()
CreateTimerQueueTimer(@timer_Q, timerQueue, @timerProc_Q(), 0, 0, 1000, 0)
CreateTimerQueueTimer(@timer_Q2, timerQueue, @timerProc_Q2(), 0, 0, 5000, 0)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 7
KillTimer_(0, timer_ST)
DisableGadget(7, 1)
Case 8
KillTimer_(0, timer_ST2)
DisableGadget(8, 1)
Case 9
timeKillEvent_(timer_SE)
DisableGadget(9, 1)
Case 10
timeKillEvent_(timer_SE2)
DisableGadget(10, 1)
Case 11
;deletes individual timer - queue still active
DeleteTimerQueueTimer(timerQueue, timer_Q, 0)
DisableGadget(11, 1)
Case 12
DeleteTimerQueueTimer(timerQueue, timer_Q2, 0)
DisableGadget(12, 1)
EndSelect
EndSelect
Until appQuit = 1
;terminates any active timers
KillTimer_(0, timer_ST)
KillTimer_(0, timer_ST2)
timeKillEvent_(timer_SE)
timeKillEvent_(timer_SE2)
DeleteTimerQueueEx(timerQueue, 0) ;deletes the entire queue
Hope it's helpful.