Timer Events

Share your advanced PureBasic knowledge/code with the community.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Timer Events

Post 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!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post by Max. »

If I want to set multiple timers, how do I know which timer was triggered?
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

When receiving the #WM_TIMER message, the wParam value holds the
TimerID you specified when creating the timer.

Timo
quidquid Latine dictum sit altum videtur
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post 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
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post by Max. »

Thanks, guys!
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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 :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Timer Events

Post 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.
BERESHEIT
Post Reply