Efficient use of timers?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Can you tell me if this is the most efficient way to do this?

In a service I created set of timers to start other timers to click every hour on the quarters of the hour (15, 30, 45 past the hour and on the hour).

I created 8 timers in all. For example. 15 past the hour:

A calculation was made to figure when the first occurrence of 15 past the hour would occur. This depended on if the curent time is on 15 past or before 15 past or after 15 past so I would calculate the remaining time needed to start the regular time that will click 1 per hour.

Is there a way to use just one timer for 15 past, and 1 timer for 30 past etc.?

Maybe I can eliminate timers all together if B4A can schedule an alarm to fire an event each hour on the quarters.

Thanks.
 

stevel05

Expert
Licensed User
Longtime User
Seems to me that one timer will suffice and you need to calculate the remaining time until the 1st event and schedule the timer for that. Then when the first timer event arrives, reschedule the time to tick every 15 mins, or recalculate it which may make it more accurate.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Steve,

Thanks for the quick reply.

Can you show some sample coding for 15 past the hour and I'll work on the other quarters?

Thanks.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi
Something like this should do the trick. If you want it more accurate you will need to test the seconds and adjust for that too.

Steve

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Timer1 As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Timer1.Initialize("Timer1",60000)
   SetTimer
End Sub

Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)
   Timer1.Enabled=False
End Sub
Sub SetTimer
   Dim Minute As Int
   Dim Hour As Int

   'Determine the interval to the next 15 minute trigger and set the timer interval

   Now=DateTime.Now
   Minute=DateTime.GetMinute(Now)
   Log (Minute)
   
   Select True
      Case (Minute < 15)
         Timer1.Interval=(15-Minute)*60000
         
      Case (Minute < 30)
         Timer1.Interval=(30-Minute)*60000
         
      Case (Minute < 45)
         Timer1.Interval=(45-Minute)*60000
         
      Case (Minute <= 59)
         Timer1.Interval=(60-Minute)*60000

      Case Else
          Log("Error setting timer")   
   End Select
   
   Log((Timer1.Interval/1000/60)&" Mins to go")

        Timer1.Enabled=True
   
End Sub

Sub Timer1_Tick
   ToastMessageShow("Timer Tick "&DateTime.Time(DateTime.Now),False)
   
   'Do whatever
   '.
   '.
   '.
   
   'Set interval for next 15 minute slot.
   Timer1.Interval=15*60000
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks so much for the help !

:D

Hi
Something like this should do the trick. If you want it more accurate you will need to test the seconds and adjust for that too.

Steve

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Timer1 As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Timer1.Initialize("Timer1",60000)
   SetTimer
End Sub

Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)
   Timer1.Enabled=False
End Sub
Sub SetTimer
   Dim Minute As Int
   Dim Hour As Int

   'Determine the interval to the next 15 minute trigger and set the timer interval

   Now=DateTime.Now
   Minute=DateTime.GetMinute(Now)
   Log (Minute)
   
   Select True
      Case (Minute < 15)
         Timer1.Interval=(15-Minute)*60000
         
      Case (Minute < 30)
         Timer1.Interval=(30-Minute)*60000
         
      Case (Minute < 45)
         Timer1.Interval=(45-Minute)*60000
         
      Case (Minute <= 59)
         Timer1.Interval=(60-Minute)*60000

      Case Else
          Log("Error setting timer")   
   End Select
   
   Log((Timer1.Interval/1000/60)&" Mins to go")

        Timer1.Enabled=True
   
End Sub

Sub Timer1_Tick
   ToastMessageShow("Timer Tick "&DateTime.Time(DateTime.Now),False)
   
   'Do whatever
   '.
   '.
   '.
   
   'Set interval for next 15 minute slot.
   Timer1.Interval=15*60000
End Sub
 
Upvote 0
Top