FreeTimer timer

Parameters

timer - a timer handle returned by CreateTimer

Description

Destroys a timer and releases the resources behind it.

Timers are cheap but they are not free, and each one holds a small amount of system machinery. Free a timer as soon as you stop needing it - when a level ends, when a cutscene finishes, or when you switch from a menu loop to the game loop with a different tick rate.

The handle is dead after this call. Passing it to WaitTimer again is a mistake, so set the variable to 0 afterwards if there is any chance of using it by accident later.

Most games create a single timer at startup and never free it, which is perfectly fine - everything is released when the program ends. FreeTimer matters when you create timers repeatedly, since leaking one per level adds up.

See also: CreateTimer, WaitTimer, MilliSecs, Delay.

Example

; FreeTimer Example
; -----------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; FreeTimer destroys a timer made with CreateTimer and releases its
; memory. Here a 60 Hz timer paces the loop; press Space to FreeTimer
; it and watch the loop (and the square) run flat out.

timer=CreateTimer(60)

While Not KeyDown(1)

    Cls

    ; Space toggles: free the timer, or create a fresh one
    If KeyHit(57) Then
        If timer<>0 Then
            ; The documented command: destroy the timer
            FreeTimer timer
            timer=0
        Else
            timer=CreateTimer(60)
        EndIf
    EndIf

    x=(x+4) Mod 640
    Rect x,220,40,40,True

    Text 0,0,"Space: FreeTimer / CreateTimer again   Esc: exit"
    If timer<>0 Then
        Text 0,20,"Timer running: loop locked to 60 fps"
    Else
        Text 0,20,"Timer freed: loop runs as fast as it can"
    EndIf

    ; Flip False: don't wait for the monitor, so the difference shows
    Flip False

    ; Only wait while the timer still exists - never use a freed timer
    If timer<>0 Then WaitTimer(timer)

Wend

End

Index