Problem with timer.

BlitzMax Forums/BlitzMax Beginners Area/Problem with timer.

Haramanai(Posted 2006) [#1]
If I run this code and then tap fast the space bar it comes a time that the TimerTicks stucks at 0
Strict

Local timer:TTimer = CreateTimer(60)

Graphics 640 , 480
While Not KeyHit(Key_Escape)
	Cls
	DrawText TimerTicks(Timer) , 10 , 10
	
	If KeyHit(KEY_SPACE) Then timer = CreateTimer(60)
	
	Flip
Wend



Grisu(Posted 2006) [#2]
I think you have to free the old timer before creating a new one.

Strict

Local timer:TTimer = CreateTimer(60)

Graphics 640 , 480
While Not KeyHit(Key_Escape)
	Cls
	DrawText TimerTicks(Timer) , 10 , 10
	
	If KeyHit(KEY_SPACE) Then 
           timer = Null      
	   timer = CreateTimer(60)
	EndIf 
	Flip
Wend



Haramanai(Posted 2006) [#3]
No the problem still here.


Grisu(Posted 2006) [#4]
FlushKeys() doesn't as well.

The problem seems to be that bmx itself needs a millisec for the timer to be created. Once you create them faster than bmx can handle it it comes to an overflow.

Ahm. Why would you need to create a timer that often?
This is not good coding at all creating items so often.


FlameDuck(Posted 2006) [#5]
I think you have to free the old timer before creating a new one.
No you don't.
Strict

Local timer:TTimer = CreateTimer(60)

Graphics 640 , 480
While Not KeyHit(Key_Escape)
	Cls
	DrawText TimerTicks(timer) , 10 , 10
	DrawText timer.toString(),10,20
	
	If KeyHit(KEY_SPACE) Then timer = CreateTimer(60)
	
	Flip
Wend
If you try this code you can see it creates a new timer each time.


Haramanai(Posted 2006) [#6]
Just expirimenting.


FlameDuck(Posted 2006) [#7]
The problem seems to be that bmx itself needs a millisec for the timer to be created. Once you create them faster than bmx can handle it it comes to an overflow.
I thought that too, however creating five or six in a row, works aswell. More likely it's a (Windows?) limitation on ammount of concurrent Timers?

This code seems to work:
Strict

Local timer:TTimer = CreateTimer(60)

Graphics 640 , 480
While Not KeyHit(Key_Escape)
	Cls
	DrawText TimerTicks(timer) , 10 , 10
	DrawText timer.toString(),10,20

	
	If KeyHit(KEY_SPACE) 
		StopTimer timer
		timer = CreateTimer(60)
	EndIf
	Flip
Wend