Android Question More than one timer starting

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Community,

I need a timer for several participants that triggers an action (e.g. ringing the bell) for these people after a different time.

This works fine with a timer for 1 person.

Does anyone have a little code example how to start several timers (threads) at different times for several people (starters)?
Thank you in advance for your help!
 

udg

Expert
Licensed User
Longtime User
I didn't fully understand you goal, but did you consider a single timer with a resolution low enough to be used for all of the "users"?
I mean, if your timer ticks once per minute, in its sub I can verify which users need to be called based on counters specific for each user (e.g. user A once every 5 ticks, meaning once every 5 minutes, user B once evry 12 ticks and so on).
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I do not know if there is a limit on the number of Timers.
No limit except processing power and (highly unlikely) memory. As Timer code is called from the message loop and runs synchronously on the main thread too many Timers may overload the main thread and delay other Timers being executed.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another option that can be sometimes easier to manage is to have a single timer that ticks fast enough and add the tasks to a list.
The task is a custom type that includes the event time. Every tick you check whether any of the queued tasks should be fired. It will be very quick, assuming that there aren't too many tasks. If there are (and there aren't) then you can store them in a sorted list and only check the first one.
 
Upvote 2

JOTHA

Well-Known Member
Licensed User
Longtime User
Because i am currently very busy in my job, so I can answer so late.
Thank you very much for the proposed solutions.

I think that Erel's suggestion is the closest solution for my project.

I need 10-15 timers that run between 10-12 minutes and then trigger an event.
Each timer is assigned to a person who should then be listed in the order of the events.

I will try this way as soon as I have more time.

I do not know if there is a limit on the number of Timers.
@klaus: There is no limit, but it is realistic, that not more than 15 timers are necessary at the same time.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I will try this way as soon as I have more time
Well - I had some free time and this looked like an interesting problem so I have written a demo.

You absolutely need only one timer! This app manages twenty buttons and highlights them individually for one second every ten seconds according to the order in which they are first activated. Button events (on and off) are held in a list and the timer checks the list ten times a second. It could handle a lot more buttons, and really does not need to check status so frequently.

One important proviso - everything falls apart if the app moves out of the foreground, but that is a factor that you will have to handle carefully whatever timing system you use.
 

Attachments

  • Triggers.zip
    9.8 KB · Views: 148
Upvote 0
Top