Android Question Use of Timers

Discussion in 'Android Questions' started by Harris, Mar 30, 2015.

  1. Harris

    Harris Well-Known Member Licensed User

    After going thru my code and cleaning up (massive) warnings of un-used vars.

    I don't understand why timers should be declared in process globals (warning 13)?

    I use timers everywhere in activities and service mods... I usually call them the same name...
    I pause (disable) and resume them in activities as required. They seem to work fine in this manner.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The first question is why not? Generally speaking all non-UI objects should be declared in Process_Globals.

    If you are careful and always pause the timers in Activity_Pause then it will work fine. The problems start when you don't pause the timer. Now whenever the orientation changes (for example) a new timer will be created.
     
  3. thedesolatesoul

    thedesolatesoul Expert Licensed User

    The wording here confused me a bit. You are talking about this happening when a Timer is declared in Globals, right?
     
    Peter Simpson, lemonisdead and NJDude like this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  5. thedesolatesoul

    thedesolatesoul Expert Licensed User

    I also suggest using Timers only in Process_Globals. They might seem to work 99% of the time in Globals but when you hit a problem you might be forced to re-design a lot of things.

    Are timers indestructable though? They seem to carry no context and nobody holds a reference to them?
     
    lemonisdead likes this.
  6. Erel

    Erel Administrator Staff Member Licensed User

    Timers like all other objects are released from memory when there are no live references to the timer object. As long as a timer is enabled there will be a live reference to the timer (from the message queue).
     
    Peter Simpson and lemonisdead like this.
  7. thedesolatesoul

    thedesolatesoul Expert Licensed User

    What you are saying makes complete sense to me, but I dont understand how it works.
    When the activity dies, the message queue doesnt, so the timers in globals stay alive/cannot be GC. The timers in Process_Globals will have their references intact.

    So are you saying even after the timer message has been posted (and handled), the message queue still holds a reference to the timer?
    How does this happen, isnt the message queue just a handler?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    If the timer is enabled then it will not be GC. The problem with global timers is that later when the activity is recreated a new timer will be created.
    You can easily find yourself with multiple timers ticking instead of a single one.

    If the timer is a process global then a new timer will not be created when the activity is created.

    If the timer is not paused then it will post a new message each time that it ticks.
     
  9. thedesolatesoul

    thedesolatesoul Expert Licensed User

    So what I am trying to understand that when a Timer is a Global variable, why doesnt it get GC'd when the Activity is killed. What is holding a reference to this timer, and how, and why?
    Probably the answer I'm looking for is that a static class will only be GC'd when the process dies.
     
  10. Harris

    Harris Well-Known Member Licensed User

    Code:
    Sub Activity_Pause (UserClosed As Boolean)

        
    If UserClosed Then
           Awake.ReleasePartialLock
        
    End If 
        Tim1.Enabled = 
    False

    End Sub

    Sub Activity_Resume
        
    If Tim1.IsInitialized = False Then
           Tim1.Initialize(
    "Tim1",  timint)
           Tim1.Enabled = 
    True
        
    Else
           
    If Tim1.Enabled = False Then
              Tim1.Interval = timint
              Tim1.Enabled = 
    True
              UPDbutton
           
    End If
        
    End If
    End Sub
    This is with timer in Globals... It works well enough but I certainly want to avoid all dangers...

    Good advice.. I shall clean up and put non-UI in process_globals

    Thanks all.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    It will be released if you properly disable it when the activity is paused. Otherwise you will have a "timer leak".
     
  12. thedesolatesoul

    thedesolatesoul Expert Licensed User

    That doesnt really answer the question. Not really sure how else to word it so ill just drop it.
    Thanks
     
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