Page 1 of 1

Timer example

Posted: Wed Jun 27, 2001 9:54 am
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Code updated for 5.20+ (same as AddWindowTimer())

As i needed it, here is a simple timer for PB Windows version.

Code: Select all

;
; a simple timer example
;

If OpenWindow(0, 400, 200, 200, 100, "Simple timer example", #PB_Window_SystemMenu)
 
  SetTimer_(WindowID(0), 0, 2000, 0) ; 2000= 2 seconds
 
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #WM_TIMER
      If StartDrawing(WindowOutput(0))
        DrawText(20, 20,"Timer finished.", #Black, #White)
        StopDrawing()
      EndIf
    EndIf
   
  Until EventID = #PB_Event_CloseWindow
EndIf
End 

Mr Skunk

Edited by - mr.skunk on 27 June 2001 10:55:52

Posted: Sat Aug 25, 2001 12:25 am
by BackupUser
Restored from previous forum. Originally posted by PB.

Nice tip, Mr Skunk. But do you know if I can have 2 or even 3 timers per app?


Edited by - PB on 20 September 2001 04:21:30

Posted: Thu Sep 06, 2001 12:35 am
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Yes of course, and you can do nice things with timers.
Look at this example.

Code: Select all

; 
; a simple timer example
; 

If OpenWindow(0, 400, 200, 200, 100, "Simple timer example", #PB_Window_SystemMenu)
  
  SetTimer_(WindowID(0), 0, 2000, 0) ; 2000= 2 seconds
  
  SetTimer_(WindowID(0), 1, 1000, ?ok); after 1 second, timer 1 connect directly to the "ok" sub-program
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #WM_TIMER
      If StartDrawing(WindowOutput(0))
        DrawText(20, 20, "Timer 0 finished.")
        StopDrawing()
      EndIf
    EndIf
  Until EventID=#PB_Event_CloseWindow
EndIf
End

ok:
  If StartDrawing(WindowOutput(0))
    DrawText(20, 40, "Timer 1 finished.")
    StopDrawing()
  EndIf
Return

Edited by - mr.skunk on 06 September 2001 01:35:59

Posted: Thu Sep 20, 2001 6:31 am
by BackupUser
Restored from previous forum. Originally posted by Rings.

Second Example(Multi Timer) Crashes under NT4 SP6

Siggi

Posted: Thu Sep 20, 2001 8:38 am
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Hi.

Fred has to add the callback function into PB, so I think there is the problem here.
The timers crash even with W98 if you use two callback timers at a time.

Here is another way to do timers.

Code: Select all

; 
; timer example with NO callback
; 

#PB_Window_ThickFrame=$40000
#WS_MAXIMIZE = $1000000 

If OpenWindow(0, 400, 200, 250,140, #PB_Window_ThickFrame | #PB_Window_SystemMenu ,"Simple timer example")

  SetTimer_(windowid(),1,2000,0) ; 2000= 2 seconds
  SetTimer_(windowid(),2,1000,0) ; 1 second
  SetTimer_(windowid(),3,5000,0) ; 5 seconds

  tim1=0:tim2=0:tim3=0

  Repeat

    EventID.l = WaitWindowEvent()
    If EventID=#WM_TIMER
      DrawingOutput(WindowID())
      Select EventwParam()
        Case 1
          tim1+1
          locate(20,20):drawtext("Timer 1 called "+str(tim1)+" times (/2sec)")
        Case 2
          tim2+1
          locate(20,40):drawtext("Timer 2 called "+str(tim2)+" times (/1sec)")
        Case 3
          tim3+1
          locate(20,60):drawtext("Timer 3 called "+str(tim3)+" time and killed")
          KillTimer_(windowid(),3)
        Default
      EndSelect
      stopdrawing()
    EndIf
  Until EventID=#PB_EventCloseWindow
EndIf
End 
Tell me if it works correctly.

Hope it helps


Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st

Edited by - mr.skunk on 20 September 2001 09:41:32

Posted: Thu Sep 20, 2001 1:50 pm
by BackupUser
Restored from previous forum. Originally posted by Rings.

works perfect, but one question :
I think this is a 'normal Windowstimer' (10ms on Winnt,55ms on Win9x)
Can this been done with the muldimediatimer (1 ms ) ?

Siggi

Posted: Thu Sep 20, 2001 4:35 pm
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Don't understand your question.

Can you explain please?

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st

Posted: Fri Sep 21, 2001 11:22 am
by BackupUser
Restored from previous forum. Originally posted by Rings.

Okay i think it must be a standard Windows-Timer(While sending WM_Timer-Message)


But In the WindowsAPI there is a Multi-media-Timer available which requieres a Callback-Adress.This Timer can be fired within a 1 ms period.

VB-Definition:
Public Const TIME_ONESHOT = 0
Public Const TIME_PERIODIC = 1
Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long

LongReturn=timeSetEvent(1,0,AdressOfMyFunction,0,0)



Siggi

Posted: Fri Sep 21, 2001 3:42 pm
by BackupUser
Restored from previous forum. Originally posted by Franco.

Hi Mr.Skunk,
I was wondering that you don't have a KillTimer command in your source.
Thought it is necessary to release resources to the system.
(well you know already I'm not a professional, so I can be wrong...)

Anyway, if anybody want to use it, have to change the line:
SetTimer_(windowid(),0,2000,0) ; 2000= 2 seconds
to:
Timer1=SetTimer_(windowid(),0,2000,0) ; 2000= 2 seconds
Maybe Timer1.l is better, I'm not 100% sure about that...

And before the 'End' command he has to add the line:
KillTimer_(windowid(),Timer1)

That's it.



Have a nice day...
Franco

Posted: Fri Sep 21, 2001 5:43 pm
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Hi

Sorry don't know for the moment about multimedia timers. I've not these api doc and MS library can't access the page for the moment...
if you have the complete api docs about multimedia timers, tell me.

Franco, for the system timer, as they are created within the same process than your app, i think they are killed when your app ends (in the normal way, all the ressources used by an app are freed at the end of the app). But killing it is not a bad choice too (we have to be carefull with windows...)

But for mulimedia timers, as they are in separated threads i'm not sure at all they are destroyed at the end of the app.

As the SetTimer_() returns the id you used in the timer and not a memory address, you are right, it's not necessary to use the "Long" format for timer1.

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st