Android Question Timer in an array linked sub

Discussion in 'Android Questions' started by Terence Agius, Feb 13, 2018.

  1. Terence Agius

    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()
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    Set the event name parameter to "Timer"
     
  3. Terence Agius

    Terence Agius Member Licensed User


    Sure. Is there an example ?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. Terence Agius

    Terence Agius Member Licensed User

    Code:
    sub Process_Globals
       
       
    Private gTimer1, gTimer2, gTimer3 As Timer
    I want that only 1 timer_click() sub gets executed for all the created timers
     
  6. udg

    udg Expert Licensed 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
     
  7. Terence Agius

    Terence Agius Member Licensed User

    Now I get it; but then how would I distinguish between the different timers? The idea is that I can know which timer was triggered
     
  8. udg

    udg Expert Licensed 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.
     
    Peter Simpson likes this.
  9. Terence Agius

    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
     
  10. udg

    udg Expert Licensed 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).
     
    Peter Simpson likes this.
  11. Terence Agius

    Terence Agius Member Licensed User

    The disappointment is that with the multiple timer scenario, I can't add timers dynamically (ie create a timer only when needed)
     
  12. udg

    udg Expert Licensed 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)
    Code:
    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.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    See the video. It explains it.
     
  14. Terence Agius

    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
     
  15. Peter Simpson

    Peter Simpson Expert Licensed 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...
    Code:
    Sub Process_Globals
        
    Private Timers(5As Timer
        
    Private TmrIntervals() As Int = Array(2000300070001100013000)
    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: Feb 14, 2018
    Filippo likes this.
  16. LucaMs

    LucaMs Expert Licensed User

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

    So, if you create a similar map...

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

    ' or

    If Value...


    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
     
    Erel likes this.
  17. Erel

    Erel Administrator Staff Member Licensed User

    I agree with Luca (which agrees with me...). The best solution here is to use a Map.
     
    LucaMs likes this.
  18. LucaMs

    LucaMs Expert Licensed User

    :D
     
    KMatle likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice