Page 1 of 1

Timer Events

Posted: Sat Sep 06, 2003 4:25 am
by Karbon
Code updated For 5.20+

Probably known well by others but I ran across this today searching the forum and thought I'd bring it up again

HighResTimer userlib has a timer in it but this is a little different

Code: Select all

settimer_(WindowID,TimerID,Time_in_ms,0)
That will send a #WM_TIMER event every Time_in_ms

Code: Select all

killtimer_(WindowID,TimerID)
That will stop the timer..

Pretty handy!

Posted: Sat Sep 06, 2003 10:47 am
by Max.
If I want to set multiple timers, how do I know which timer was triggered?

Posted: Sat Sep 06, 2003 3:01 pm
by freak
When receiving the #WM_TIMER message, the wParam value holds the
TimerID you specified when creating the timer.

Timo

Posted: Sat Sep 06, 2003 3:13 pm
by blueb
I found this source code in my PB folders. It was written by Danilo, I searched the PureBasic Forum, but couldn't locate it, so I'm including it here. It shows 2 timers in use at one time.

Code: Select all

;-------------------------------------------------
; Another great little demo from Danilo on timers
; and scrolling.
;-------------------------------------------------

Global myText$
Global hWnd.l
Global TextLength.l
#ScrollSpeed_in_Milliseconds = 70


myText$ = "     This is a little Scroller-Test with the Timer by Danilo     "
TextLength = Len(myText$)


Procedure Scroller()
;Shared Scroller_a
Shared Scroller_Position
Shared Scroller_Direction

If Scroller_Position < TextLength + 1 And Scroller_Direction = 0
    TEMP$ = Right(myText$,Scroller_Position)
    SetWindowText_(hWnd, TEMP$)
    Scroller_Position+1
Else
    Scroller_Direction = 1
    TEMP$ = Right(myText$,Scroller_Position)
    SetWindowText_(hWnd, TEMP$)
    Scroller_Position-1
    If Scroller_Position = 0 : Scroller_Direction = 0 : EndIf
EndIf
EndProcedure


Procedure mybeep()
  beep_(500,1000)
  EndTimer(0)
  DisableGadget(0,0)
  DisableGadget(1,0)
  StartTimer(1, #ScrollSpeed_in_Milliseconds, @Scroller())
EndProcedure


Procedure Alarm()
  beep_(500,60)
EndProcedure


MessageRequester("Minimal Timer Resolution",Str(GetMinTimerResolution()),0)
MessageRequester("Maximal Timer Resolution",Str(GetMaxTimerResolution()),0)


hWnd = OpenWindow(0, (GetSystemMetrics_(#SM_CXSCREEN)-400)/2,(GetSystemMetrics_(#SM_CYSCREEN)-100)/2, 400, 100, #PB_Window_SystemMenu,"")


  If CreateGadgetList(WindowID())
    ButtonGadget(0,  10, 10, 100, 25, "Quit Scroller")
    DisableGadget(0,1)
    ButtonGadget(1, 120, 10, 250, 25, "Change Timer from Scroller to Beep")
    DisableGadget(1,1)
  EndIf
SetForeGroundWindow_(hWnd)


StartTimer(0,1000, @mybeep())

;message loop
  Repeat
           EventID.l=WaitWindowEvent()
              If EventID = #PB_EventGadget
                 Select EventGadgetID()
                  Case 0 ; End the Timer
                    A$ = GetGadgetText(0)
                    If A$ = "Quit Alarm"
                       SetGadgetText(0,"Exit Program")
                       DisableGadget(1,1)
                    EndIf
                    If A$ = "Exit Program"
                       Quit = 1
                    EndIf
                    EndTimer(1)
                  Case 1
                    SetGadgetText(0,"Quit Alarm")
                    StartTimer(1, 100, @Alarm())
                 EndSelect
              EndIf
              ; pressed CloseButton or ALT+F4 ??
              If EventID = #PB_EventCloseWindow
                 Quit = 1
              EndIf
  Until Quit = 1
; the end

Posted: Sat Sep 06, 2003 3:14 pm
by Max.
Thanks, guys!

Posted: Sat Sep 06, 2003 9:25 pm
by Karbon
Doh. Sorry for not explaining detecting the events. I came in from the bar a little late last night if you know what I mean :-)

Re: Timer Events

Posted: Fri Mar 24, 2017 8:25 am
by netmaestro
It's low res though. For API, TimeSetEvent_() will perform to the single millisecond and even the native ElapsedMilliseconds() will do likewise since its update a couple of years back. With SetTimer_() the best granularity you will achieve is the OS refresh rate, which is around 15ms.