problem timer

jota

Active Member
Licensed User
Longtime User
'''

Sub Globals
Dim timer1 As Timer
Dim ctr As Boolean : ctr = True
End Sub

Sub Activity_Create(FirstTime As Boolean)


timer1.Initialize("timer1",1000)

timer1.Enabled = True
ToastMessageShow("Timer On",False)

Do While ctr
DoEvents
Loop

timer1.Enabled = False
ToastMessageShow("Timer Off",False)

End Sub

Sub timer1_tick
ctr = True
End Sub


'''

Hoping to make a pause control for time
this code does not work. What I can do? . Thanks.
 

stevel05

Expert
Licensed User
Longtime User
What exactly are you trying to achieve? You are never setting ctr to false so it will repeat the Do loop forever.

If you want to exit the loop after a certain amount of time, then you need a global variable set to count each time the tick event is fired, then test and exit when > time.

For instance something like:

Sub Globals
Dim timer1 As Timer
Dim ctr As Boolean : ctr = True
Dim Timer1Count As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)


timer1.Initialize("timer1",1000)

Timer1Count=0

timer1.Enabled = True
ToastMessageShow("Timer On",False)

Do While ctr
DoEvents
Loop

End Sub


Sub timer1_tick
If Timer1Count >= 4 then 'Will wait for 4 ticks
ctr = false
Timer1.Enabled=False
ToastMessageShow("Timer Off",False)
Timer1Count=0
Return
End If

Timer1Count=Timer1Count+1


End Sub

Although I'm not sure if this will upset the OS and be greeted with a Stopped responding / Force close message if the pause is too long.

Steve
 
Last edited:

jota

Active Member
Licensed User
Longtime User
to copy the example I was wrong in the sub timer must equal false.

Sub timer1_tick
ctr = False
End Sub

I tried your example and also has worked for me

I want to do a wait of time allocated to the timer. thanks


-------------------------------------
my English is google translator sorry
 

jota

Active Member
Licensed User
Longtime User
I tested the solution stevel05 friend, but not worked. I see the problem as the DoEvents command.You could include an example that works Thanks.
 

stevel05

Expert
Licensed User
Longtime User
If you can let us know what you are trying to do with the code, there may be a simpler solution.

Steve
 

jota

Active Member
Licensed User
Longtime User
Example 1

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim bucle,bucle2 As Int
Dim lbl(5) As Label
Dim timer1 As Timer
Dim vTop As Int
Dim ctr As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
For bucle2 = 0 To 4
lbl(bucle2).Initialize("")
Next

vTop = 0

For bucle2 = 0 To 4
activity.AddView(lbl(bucle2),0,vTop,100%x,50dip)
lbl(bucle2).Text = "TEXTO DE EJEMPLO Nº " & bucle2
SLEEP(1000) '< comment this line to see the difference - comentar esta linea para ver la diferencia
vtop = vtop + 60dip
Next

End Sub

Sub Sleep(t As Int)

#Region: Original code - Codigo original
'ctr = True
'timer1.Initialize("Timer1",t)
'timer1.Enabled = True
'Do While ctr
' DoEvents
'Loop
#End Region

#Region: botched job - chapuza
For bucle = 0 To t
DoEvents
Next
#End Region

timer1.Enabled = False
End Sub
Sub timer1_tick
ctr = False
End Sub
 

jota

Active Member
Licensed User
Longtime User
Example 2

Sub Globals
Dim timer1 As Timer
Dim ctr As Boolean : ctr = True
Dim lbl As Label
Dim ctr As Boolean
Dim a As Animation
Dim tiempo As Int
Dim bucle As Long
End Sub

Sub Activity_Create(FirstTime As Boolean)

tiempo = 1000

activity.Initialize("")
activity.Color = Colors.ARGB(158,125,168,154)
lbl.Initialize("")
activity.AddView(lbl,0,50%y,100%x,50dip)
lbl.Text = "TEXTO DE PRUEBA"
lbl.TextSize = 30dip

' I know there are other ways to do this, but this would be an example of the usefulness
' ----------------------------------------------------------------------------------------
' Se que hay otras formas de hacerlo, pero esto seria un ejemplo de la utilidad

a.InitializeTranslate(lbl,0,0,0,25%y)
a.Duration = tiempo
a.RepeatCount = 0
a.Start(lbl)

'The optimum would be that the animation library given the option not to restart the position of the object.
'------------------------------------------------------------------------------------------------------------
' Lo optimo seria que la libreria animatión diera la posibilidad de no reiniciar la posicion del objeto.

Sleep(tiempo) '< comment this line to see the difference - comentar esta linea para ver la diferencia


LBL.Top = LBL.Top + 25%Y

End Sub

Sub Sleep(t As Int)

#Region: Original code - Codigo original
'ctr = True
'timer1.Initialize("Timer1",t)
'timer1.Enabled = True
'Do While ctr
' DoEvents
'Loop
#End Region

#Region: botched job - chapuza
For bucle = 0 To t -979
DoEvents
Next
#End Region

timer1.Enabled = False
End Sub
Sub timer1_tick
ctr = False
End Sub
 

jota

Active Member
Licensed User
Longtime User
The pause to think it is very useful in many other functions, such as waiting miertras an activity is in progress, or give the user response time. I feel that my English is weird but I use google translator to communicate. Thanks
 

stevel05

Expert
Licensed User
Longtime User
You shouldn't have to worry about either of those, when there are no running subroutines, the device will just be waiting for input. Once the user presses a button on the screen it'll do it's assigned task. If you need to force the user to wait until something is complete before taking another action, you can temporarily disable the views button.enabled=False etc.

Steve
 

jota

Active Member
Licensed User
Longtime User
Hi steve, I've found the problem of my "experiments " within a loop [while ... loop] or [for ... next] the timer and the services stop working.
If a loop exists in a service timer module of code still works, but the other services stop.

Not if it is wrong or should be so, but it is a problem that limits a lot. Thanks again.
--------------------------------------------------------------------------------------
Hola steve, ya he encontrado el problema de mis "experimentos", dentro de un bucle [while...loop] o [for...next] el timer y los servicios dejan de funcionar.
Si el bucle existe en un servicio el timer del modulo de codigo sigue funcionando, pero el resto de los servicios se paran.

No se si es un error o debe de ser asi, pero no es un problema que limite mucho. Gracias otra vez.
 

stevel05

Expert
Licensed User
Longtime User
HI Jota,

Looking at your example 2, it seems that you are setting a timer, and them trying to fill the time until the timer kicks in, which is unnecessary. Once your subroutine has finished, the program will happily wait until the timer tick arrives.

Unless I misunderstood your code, I think that this is what you are after.

Sub Globals
Dim timer1 As Timer
Dim ctr As Boolean : ctr = True
Dim lbl As Label
Dim ctr As Boolean
Dim a As Animation
Dim tiempo As Int
Dim bucle As Long
End Sub

Sub Activity_Create(FirstTime As Boolean)

tiempo = 1000

activity.Initialize("")
activity.Color = Colors.ARGB(158,125,168,154)
lbl.Initialize("")
activity.AddView(lbl,0,50%y,100%x,50dip)
lbl.Text = "TEXTO DE PRUEBA"
lbl.TextSize = 30dip



' I know there are other ways to do this, but this would be an example of the usefulness
' ----------------------------------------------------------------------------------------
' Se que hay otras formas de hacerlo, pero esto seria un ejemplo de la utilidad

a.InitializeTranslate(lbl,0,0,0,25%y)
a.Duration = tiempo
a.RepeatCount = 0
a.Start(lbl)

'Timer should get it's 1st tick as the animation is finishing
Timer1.Initialize("Timer1",tiempo)
Timer1.Enabled=True

'The optimum would be that the animation library given the option not to restart the position of the object.
'------------------------------------------------------------------------------------------------------------
' Lo optimo seria que la libreria animatión diera la posibilidad de no reiniciar la posicion del objeto.

End Sub

Sub timer1_tick
LBL.Top = LBL.Top + 25%Y
Timer1.Enabled=False
End Sub
 
Top