Android Question Timer in an array linked sub

Terence Agius

Member
Licensed User
Hi guys,

I have coded in several languages in my life, but have always steered clear of OO. But here I am.
I am trying to create multiple timers, and I wish to handle them with the same Timer_tick() routine; how do I achieve this?

</code>
Type stList(nTimer As Timer, name As String, cType As String, find As String, failed As Int, passed As Int, interval As Int, id As Int)

Private arCheckList() As stList

<code>

is there something like
arCheckList(1).nTimer.TickFunction = Tick_Timer()
 

Terence Agius

Member
Licensed User
B4X:
sub Process_Globals
   
   Private gTimer1, gTimer2, gTimer3 As Timer

I want that only 1 timer_click() sub gets executed for all the created timers
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Terence,
as @Erel suggested in post #2 above, just use the same name in the Init clause of your timers. This way all of them will execute the same and only sub YourName_Tick .
Is your goal to execute some code at different intervals? If yes, there are other ways to achieve it. Please explain better what you're gonna do with those timers.

udg
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Tha's why I asked about your intended usage.
Let's suppose you have action1 to be executed every 10seconds, actions2 every 30secs and action3 every 50secs.
You could do it with a single timer set to 10 secs and count how many times it ticked to select one of the actions above.
 
Upvote 0

Terence Agius

Member
Licensed User
I thought of that; but I have no idea if user decides to "cancel" one of those timer actions. I am using a map which has the config of each timer and each tick would identify the timer and evaluate what to do in that slice.

My alternate is to use just one timer, but I don't see that as optimal, I would prefer to have a timer per activity and pause the timer which is not active
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I see. One way could be the obvious, having three indipendent timers, each executing its own tick code.
Another, the single timer, could be based on a tick frequency good for all of them, a counter to understand which one ticked and a test to decide if its code is to be executed or not (based on the "cancelled" status" the user could set somewhere else in your code).
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I'm sorry since I didn't bring my notebook with me so I can't code an example for you. Please use the following just as a way to inspire you further.
**Single timer (timers based on 10sec multiples)
B4X:
sub timer1_tick
   counter = counter +1 '1 = 10 sec, 2 = 20secs...
   checkInterval = counter * 10 '10,20,30..
   For each item in MyTimerLIst
     if (item.active AND item. interval = checkInterval) do execute code(timerX)
   if counter = maxcounter the counter = 0 'once it reached the max allowable interval it gets resetted
end sub
The idea here is that you use Timer_Tick to look for which timers need to execute code (based on their active/disabled state and frequency), than call a separate function which contains code snippets specific to each timer where a parameter to this function tells it which snippet to execute.
Another way is to pass to the "snippet code" function a list of timers that need execution of their code; so you use Tick just to discover which enabled timers ticked than delegate proper actions elsewhere.
 
Upvote 0

Terence Agius

Member
Licensed User
It is clever, I give you that. But if you have 100's and 90 of them are not active, it means all those CPU cycles are being wasted as program needs to cycle a 100 timers each time there is a tick.
I come from an age (I have been coding 38 years) where every clock tick counts! Alas, my optimizing gene will never die. But thank. I honestly believed a modern OO language would allow for more flexibility - especially since this is coding for a mobile device which does not have that much CPU power
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Firstly I would like to say welcome to the community...

Quote "I come from an age", you sound like Fred Flintstone :)

Maybe this will help you as you already know the intervals so you can easily catch and run/call routines depending on the timer intervals

Check out your logs...
B4X:
Sub Process_Globals
    Private Timers(5) As Timer
    Private TmrIntervals() As Int = Array(2000, 3000, 7000, 11000, 13000)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    For i = 0 To TmrIntervals.Length - 1
        Timers(i).Initialize("TmrAll", TmrIntervals(i))
        Timers(i).Enabled = True
    Next
End Sub

Sub TmrAll_Tick
    Dim Tmr As Timer = Sender
    'Log(Tim.Interval)
  
    Select Tmr.Interval
        Case 2000
            Log("Timer 1 Firing")
        Case 3000
            Log("Timer 2 Firing")
        Case 7000
            Log("Timer 3 Firing")
        Case 11000
            Log("Timer 4 Firing")
        Case 13000
            Log("Timer 5 Firing")
    End Select
End Sub

Enjoy...
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I remember a great Erel's example, where the keys of a map were... timers!

So, if you create a similar map...

B4X:
Sub TimerX_Tick
Dim tmr As Timer = Sender
Dim Value As WhatYouNeed = mapTimers.Get(tmr)
Select Value
...

' or

If Value...



I honestly believed a modern OO language would allow for more flexibility
You can create a wrapper class for timers.



P.S. "I remember a great Erel's example"... My memory is not as bad as I often think :D
 
Upvote 0
Top