Here's an example:
sync on
sync rate 0
sync sleep 1
` Want full control over program closure, so disable escape, and
` enable windows message callback to capture all messages (including WM_CLOSE)
disable escapekey
set message callback "WndProc"
` Load the User32 dll, and get function pointers for SetTimer/KillTimer
global User32_handle = 0
global SetTimer_fn as dword = 0
global KillTimer_fn as dword = 0
User32_handle = load dll("user32.dll")
SetTimer_fn = get ptr to dll function( User32_handle, "SetTimer" )
KillTimer_fn = get ptr to dll function( User32_handle, "KillTimer" )
global CallCounter = 0 ` Use the timer to increment this
global TimerActive = 0 ` Set if timer is running
global AppClose = 0 ` Flag app closure via close button
repeat
` Output the current state
cls
print "Timer Counter = "; CallCounter
print "Timer Active = "; TimerActive
print ""
if TimerActive = 1
` Provide option to deactivate the timer
print "Press 'd' to deactivate timer"
if inkey$() = "d"
KillTimer(1)
TimerActive = 0
endif
else
` Provide option to activate the timer
print "Press 'a' to activate timer"
if inkey$() = "a"
SetTimer(1, 100) ` Every 10th of a second
TimerActive = 1
endif
endif
sync
` Exit when escapekey pressed, or close button clicked
until AppClose = 1 or escapekey()
` Disable the windows message callback, and disable the timer if still active
if TimerActive = 1 then KillTimer(1)
set message callback 0
` Unload the User32 dll
unload dll User32_handle
end
#constant WM_CLOSE 0x0010
#constant WM_TIMER 0x0113
function WndProc(hwnd as dword, msg as dword, wparam as dword, lparam as dword)
select msg
case WM_TIMER
inc CallCounter
endcase
case WM_CLOSE
AppClose = 1
stop current message
endcase
endselect
endfunction 0
function SetTimer(TimerId as dword, Interval as dword)
call function ptr SetTimer_fn, get dbpro window(), TimerId, Interval, 0
endfunction
function KillTimer(TimerId as dword)
call function ptr KillTimer_fn, get dbpro window(), TimerId
endfunction
The main downside of this method is that every message that is passed to DBPro is passed to this function - remember that DBPro is NOT fast at this stuff, so there will be a performance impact. I've also used the DLL functions built into my plug-ins to call the SetTimer/KillTimer functions, mainly because I haven't used them for a while and wanted the practice