Calling a function/sub each X minutes?

BlitzMax Forums/BlitzMax Beginners Area/Calling a function/sub each X minutes?

Canali(Posted 2010) [#1]
Hi,
could someone explain the best way to call a function based on a timer?

I was exploring the documentation about Timer and Events but didn't fully get how to create a timer, enable/disable it, and how to call a function when the timer arrives at X minutes.

Any help is very appreciated.
Thanks.
:)


degac(Posted 2010) [#2]
This example calls MyFunction every 1/5 of seconds.

Local timer:ttimer=CreateTimer(5) 'it creates a 'tick' every 1/5 of seconds

While True
	WaitEvent
	
	Select EventID()
		Case EVENT_TIMERTICK
			MyFunction()
	End Select
	
Wend

Function MyFunction()
	Print "Here!"
End Function


This is a variation: every 1/5th of seconds the program checks if your internal timer (Timer_Main) is greater than the delay you fixed (in this case timer_delay=1000 = 1 seconds)

You can change timer_delay=1000*60 = 1 minute

Local timer:ttimer=CreateTimer(5) 'it creates a 'tick' every 1/5 of seconds
Global timer_main:Int=MilliSecs()
Global timer_delay:Int=1000

While True
	WaitEvent
	
	Select EventID()
		Case EVENT_TIMERTICK
			If MilliSecs()>timer_main+timer_delay
				MyFunction()
				timer_main=MilliSecs()
			End If
			
	End Select
	
Wend

Function MyFunction()
	Print "Here!"
End Function



Canali(Posted 2010) [#3]
Thx very much Degac.
It's very clear the way you handle 'em.

Just afew questions:

- Is it possible to handle the funciton call as a separate thread parallel to the main cicle?

- If I want to have more timers, how do I get each event?

- Do events buffer? I mean, If i don't call the "WaitEvent" (or "PollEvent") do I have a pile of uncaptured events??

- Can I pause a timer?

Thanks again.


Azathoth(Posted 2010) [#4]
- If I want to have more timers, how do I get each event?

Compare the object returned by EventSource with the timer object.

- Do events buffer? I mean, If i don't call the "WaitEvent" do I have a pile of uncaptured events??
It uses a queue.