Android Question Question about Timers

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?
 

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 ;-)
 

emexes

Well-Known Member
Licensed User
Probably best to confirm this on your hardware, just in case ;-)
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:
B4X:
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:
Logger connected to: TCL 5009A
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Timer started at 1
** Activity (main) Resume **
Timer paused at 857
Timer re-enabled at 4404
Tick at 5450
Tick at 6427
Tick at 7496
Timer interval reassigned current value at 7929
Tick at 8455
Tick at 9435
Tick at 10433
Tick at 11438
Timer interval assigned different value and back again at 11465
Tick at 12490
Tick at 13495
Tick at 14490
Timer stopped at 14995
 

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.
 

emexes

Well-Known Member
Licensed User
When enabling a timer that is already enabled
the question is "what happens if you enable it while it is running
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.

:)
 

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? :)
 
Top