I have been using B4A for several years but have never noticed the following behaviour before.
If a class with a timer is initialised multiple times, the timer seems to be duplicated on each call to the class' Initialize sub. The following B4XMainPage initialises such a class every time Button1 is clicked:
ClassWithTimer:
The following is the output from the above. As expected, on first clicking the Button an event is generated every 4 seconds. However, clicking the Button again results in an event, roughly, every 2 seconds. The more the clicks, the more frequent the events.
It would seem that each time the class is initialised, a new instance of the class is created and the previous instance is orphaned. I had always thought that re-initialising a class instance would just "reset" that instance of the class, rather than create a new instance. Assuming my new understanding is correct, is there a way to destroy the current instance before re-initialising? Of course, the work around is only ever to initialise an instance once.
If a class with a timer is initialised multiple times, the timer seems to be duplicated on each call to the class' Initialize sub. The following B4XMainPage initialises such a class every time Button1 is clicked:
B4X:
Sub Class_Globals
Private Root As B4XView
Private aClassWithTimer As classWithTimer
Private cnt As Int=1
End Sub
Public Sub Initialize
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
End Sub
Private Sub Button1_Click
aClassWithTimer.Initialize
aClassWithTimer.Message="Button clicked " & cnt & " times"
cnt=cnt+1
End Sub
B4X:
Sub Class_Globals
Public logMessage As Timer
Public theMessage As String
End Sub
Public Sub Initialize
logMessage.Initialize("logMessage", 3000)
logMessage.Enabled=True
End Sub
public Sub setMessage(msg As String)
theMessage=msg
End Sub
Sub logMessage_Tick
Log(DateTime.Time(DateTime.Now) & " " & theMessage)
End Sub
It would seem that each time the class is initialised, a new instance of the class is created and the previous instance is orphaned. I had always thought that re-initialising a class instance would just "reset" that instance of the class, rather than create a new instance. Assuming my new understanding is correct, is there a way to destroy the current instance before re-initialising? Of course, the work around is only ever to initialise an instance once.
B4X:
09:35:45 Button clicked 1 times
09:35:49 Button clicked 1 times
09:35:53 Button clicked 1 times
09:35:57 Button clicked 1 times
09:36:01 Button clicked 1 times
09:36:05 Button clicked 2 times
09:36:06 Button clicked 2 times
09:36:09 Button clicked 2 times
09:36:10 Button clicked 2 times
09:36:13 Button clicked 2 times
09:36:14 Button clicked 2 times
09:36:17 Button clicked 3 times
09:36:18 Button clicked 3 times
09:36:19 Button clicked 3 times
09:36:21 Button clicked 3 times
09:36:22 Button clicked 3 times
09:36:23 Button clicked 3 times
09:36:25 Button clicked 3 times
09:36:26 Button clicked 3 times
09:36:27 Button clicked 3 times
09:36:29 Button clicked 4 times
09:36:30 Button clicked 4 times
09:36:31 Button clicked 4 times
09:36:33 Button clicked 4 times
09:36:33 Button clicked 4 times
09:36:34 Button clicked 4 times
09:36:35 Button clicked 4 times
09:36:37 Button clicked 4 times
09:36:37 Button clicked 4 times