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.
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.
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?
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).
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?
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.
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.
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
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?