[Q] Threads in B4A

thedesolatesoul

Expert
Licensed User
Longtime User
Hi Guys,
I have a question regarding threads in B4A.
I know that a B4A activity will have a single UI thread.
My question is what is this thread called and how do I access it?
(for eg in Java you have a 'run' thread)
So far I have been using a Timer which works fine for me. But also, is this timer part of the same thread?
The Android SDK states that you should keep the UI thread relatively free, so I guess we have to use the Threading library for that. But then can you run a timer on a separate thread?
Any guidance and discussions on this are welcome.
Thanks!
 

agraham

Expert
Licensed User
Longtime User
My question is what is this thread called and how do I access it?
Why would you want to access the main thread object? Messing with it would upset Android which does a lot of work for your app on the main UI thread posting messages to itself on its message queue.

Timers run on the main UI thread and timer events are invoked by posting delayed messages to its message queue.

But then can you run a timer on a separate thread?
No but you can use Thread.Sleep() to achieve a similar effect.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The UI can only be accessed from the UI thread.
All of the "slow" operations like networking, calling web services, Bluetooth and others are implemented with background threads.

The problem with blocking the UI thread is that messages in the message queue are not handled which make the program unresponsive. Timers usually do not block the message queue and will not hurt the responsiveness (assuming that Timer_Tick sub doesn't take too long).
Timers work by sending a message to the message queue. So it might take more than the "interval" value for the timer to tick, but at least the message queue is not "starved".
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Thanks for the information. That is very interesting.
...so when you are processing from a different thread how do you update the UI thread? Passing messages or semaphores, I guess.

So, instead of using a timer (which will keep the UI thread relatively free), if I add an infinite Do...While loop in the Activity_Resume object, how does that compare? Will it hog up resources, and adding a few DoEvents here and there make much difference? Ofcourse there will be no real timing control, but it does insert your code into the main UI thread with surety?

I dont have any real use for this yet, these are just hypothetical questions so far. But I am just thinking of a performance intensive app for which the timer_tick (least granulity 1ms) is not enough.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Unless you are using the Threading library your code is always running in the UI thread.

DoEvents should only be used to allow the UI to update itself. It is not a proper replacement for letting the main thread to handle the message queue.
If you do an infinite loop then you will get an "application not responding" dialog after about 5 seconds.
 
Upvote 0
Top