A question about Timer

copiloto

Member
Licensed User
Longtime User
Hello all,

Often, I use Timer control in my code. Due to speed questions, many times am using a low value when initializing Timer:

timer1.Initialize("timer1",20)

Yet, I am feared about lowing further this values because I'm not sure which would be the behaviour of the OS in some minutes or seconds. I mean, if timer interval is too short and density of code takes longer that timer interval, could it be possible that the OS create additionals threads or definitely, Timer must wait until finishing the present thread/interval? Is it regulated by semaphores?

Thanks in advance!
 

agraham

Expert
Licensed User
Longtime User
The Timer Tick even is always run on your main thread so can only run when none of the rest of your activity code is executing. The Tick event is run when your activity receives a timer message from its message loop. The Timer then posts another message and then runs your code. If your code doesn't finish in time then it does not return to look at the message queue and so cannot be re-entered or send another timer message. So if you have a too short timer interval for your code nothing bad happens, it just runs less often than you specified.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Timers never create any threads. Timers always work in the main thread and the way they work is by sending a message (for the next scheduled time) to the message queue each time they tick.

This means that even if you set the timer to a very short period it will only tick after it finishes processing the current tick (and other messages as well).
 
Upvote 0

copiloto

Member
Licensed User
Longtime User
Thanks very much for your quick and well explained replies.

And... yes! it's a good analogy!

I have made some experiments in the past and in fact it seems not to happen nothing bad, not so when using doevents... (I think it works by threads?), which I read from forum that recommend to avoid its use. But because of some times my B4a stack went over using doevents some minutes last, I thought it could be made from the same cloth ;)

Thanks a lot!!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
doevents... (I think it works by threads?)
No, it just pumps the message queue and dispatches outstanding messages, still on the main thread, but it can cause re-entrancy problems to events and is best avoided unless you know exactly why you need to use it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
DoEvents should only be used to allow the views to redraw themselves. It can be useful for example if you show a ProgressDialog and then run a length loop. In this loop you can call DoEvents to allow the ProgressDialog to spin. When called in a loop it is recommended to avoid calling it every iteration as it will slow down execution.
 
Upvote 0

copiloto

Member
Licensed User
Longtime User
DoEvents should only be used to allow the views to redraw themselves. It can be useful for example if you show a ProgressDialog and then run a length loop. In this loop you can call DoEvents to allow the ProgressDialog to spin.

Ok, I understand.

When called in a loop it is recommended to avoid calling it every iteration as it will slow down execution.

Do you mean something like this?

for a=0 to 1000
Here YES
for b=0 to 1000
Here NO!

...
...

Next
Next

In a simple iteration, then, I think it could be useful this:

for a=0 to 1000
if a mod 5=0 then
doevents
end if
...
...
next
 
Upvote 0
Top