How to run a Timer / StartServiceAt multiple.

JoanRPM

Active Member
Licensed User
Longtime User
For my application I need to call to an HTTP server to ask several questions.
I made an HTTP service that I can choose, by an index, the question. It works fine.
I need some of these questions to run periodically. The repetition time of each question is variable, depending on the importance of each. Also the total number of questions is undetermined, as is the server who tells me which are and how often it is repeated on each.

But I do not know how to do these repetitions.
I thought about doing it with "Timers" or "StartServiceAt" but do not know how. I do not think that's a good idea to create (in advance) about 20 services to manage all the "StartServiceAt".

Do I have to assemble n services for each process? The problem is I do not know how many are (I will receive it in the first communication) and I can not create it dynamically, I think.
Something as simple as "CallSubDelayed" would be fine if I could control the delay time (but "CallSubDelayed" does not work this way).

Anyone have any idea? and better if it were simple!

Thanks in advance, and I hope you understand my poor English.
 

Peter Simpson

Expert
Licensed User
Longtime User
Sub Globals
Dim StartTimer As Timer
StartTimer.Initialize("HttpCall", 900000) '900000 = 1000*60*15(the 15 = 15 minutes)
StartTimer.Enabled = True
End Sub

Sub HttpCall_Tick
'Put your code/or call sub in here, it will run every 15(x) minutes
End Sub

That's how I did it.
 
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
Erel.
Normally, the duration between the requests are in seconds. Maximum 5 minutes.
For example, the requests "A" every 5 seconds, request "B" every 50 seconds, and so on.
Applications that are very fast, are active short lived (only when the user is in a particular screen).

Peter.
Yes, but I need an undetermined number of timers (or whatever) and I need to do several applications with different periods.

Thanks for your interest.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use a single service.
You have two options:
- Use Service.StartForeground and have a timer for each type of request.
- Use StartServiceAt and schedule the next alarm time each time the service starts. You can use a sorted list that holds all the scheduled times and then handle the first one each time.

The first option is simpler to implement.
 
Upvote 0

gbdroid

Member
Licensed User
Longtime User
You might establish a timer with 5 seconds and increment individual counters (one for each http request) inside the timer sub.
each counter is checked if it has reached the multiple of 5s you need (10 for 50 seconds) and do in this case the necessary requests - and reset the counter to zero afterwards.
 
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
I think the second is more efficient, because I do not know the total number of tasks.
I would try to make a kind of FIFO stack, with the time of service and task.
For each service request, add the time and the task in a list and look at the service which has been originally applied the service, run the service and remove it from the list.
It's a bit complicated, but may work correctly.
Thanks, I'll try.
 
Upvote 0
Top