1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Question about Timers

Discussion in 'Android Questions' started by Didier9, Jun 11, 2019.

  1. Didier9

    Didier9 Active Member Licensed User

    When enabling a timer that is already enabled, does it restart the interval or does nothing (timer keeps running from where it was)?
    I have the habit of disabling always before enabling again to make sure I get the full the interval but maybe it is unnecessary?
     
  2. emexes

    emexes Well-Known Member Licensed User

    My recollection was that it continues timing-out the interval from the point that it was paused, ie, if you paused the timer when it was 2 seconds into a 10 second tick, and then later enabled it again, it would fire 8 seconds after you re-enabled it.

    I also have a vague recollection that to restart a full interval, you have to *change* the timer interval, ie, not just assign the same interval to it again.

    Probably best to confirm this on your hardware, just in case ;-)
     
  3. emexes

    emexes Well-Known Member Licensed User

    Well, I'm glad I said that, because my recollection seems to be wrong - the interval is restarted from 0 when the timer is re-enabled.

    With this test code:
    Code:
    Sub Process_Globals

        
    Dim StartTime As Long
        
    Dim tmr As Timer
     
    End Sub

    Sub Activity_Create(FirstTime As Boolean)

        tmr.Initialize(
    "tmr"123)
        StartTime = 
    DateTime.Now
     
        tmr.Interval = 
    1000
        tmr.Enabled = 
    True
        
    Log("Timer started at " & (DateTime.Now - StartTime))
     
        Sleep(
    800)

        tmr.Enabled = 
    False
        
    Log("Timer paused at " & (DateTime.Now - StartTime))

        Sleep(
    3500)

        tmr.Enabled = 
    True
        
    Log("Timer re-enabled at " & (DateTime.Now - StartTime))
     
        Sleep(
    3500)
     
        tmr.Interval = 
    1000
        
    Log("Timer interval reassigned current value at " & (DateTime.Now - StartTime))
     
        Sleep(
    3500)
     
        tmr.Interval = 
    1000 + 1    'change to something else
        tmr.Interval = 1000    'then back to desired value
        Log("Timer interval assigned different value and back again at " & (DateTime.Now - StartTime))
     
        Sleep(
    3500)
     
        tmr.Enabled = 
    False
        
    Log("Timer stopped at " & (DateTime.Now - StartTime))
     
    End Sub

    Sub tmr_Tick
     
        
    Log("Tick at " & (DateTime.Now - StartTime))
     
    End Sub
    produces this:
     
  4. Didier9

    Didier9 Active Member Licensed User

    Yes, I knew that :) that is the reason why I always disable it before enabling again but the question is "what happens if you enable it while it is running (without disabling it first)"!
    Maybe I have been wasting useless lines of code...

    I use a timer as a watchdog, restarting it periodically to catch an eventual timeout in some radio operation. Ideally, the timer is re-enabled at a rate faster than its period such that it never fires unless the radio dropped a packet.
    I found out that if I simply re-enable it, it will fire occasionally even with no radio issue but if I always disable it right before re-enabling it, it works as intended.
     
  5. emexes

    emexes Well-Known Member Licensed User

    Yes, I missed that subtlety... I did think it over at the time, figured that "already enabled" must be a machine-translation glitch, and the original tense been "has been enabled previously".

    Anyway, always good to test these things out and understand what's actually happening.

    I'm still pretty sure that I've encountered timers that don't reset on disable/enable. I'm wondering if perhaps that was B4J or B4I, as in: maybe B4X builds upon the OS/VM timers, and thus inherits any subtle differences.

    :)
     
  6. Didier9

    Didier9 Active Member Licensed User

    I do a lot of microcontroller programming in C and it's amazing the number of variations I have on seemingly simple, similar timer functions... Who has the time to write all that stuff? :)
     
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