Android Question B4X question about heavy sub in the background

padvou

Active Member
Licensed User
Longtime User
Hello,
I'm new working with B4XPages and would like to ask, how can I move a sub with heavy math calculations to the background and show a progressbar spinning around itself while the math sub is busy doing its thing? Thank you in advance.
 

LucaMs

Expert
Licensed User
Longtime User
No need to move the Sub.

I assume they are calculations inside For-Next (or Do While/Until) loops. Put a Sleep(30) inside one or more of them (30 is an approximate value).
Note that the Sub containing Sleep becomes a Resumable Sub, so treat it as such, in the way you execute it (using Wait For) and for the data type it returns.



The "progress bar" as you described it is not a progress bar, but something that rotates until the processing is complete. You can find some like that on the site.
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
First step is to measure the actual time that it takes the heavy sub to complete. Make sure to test it in release mode.
The actual time Could be from a few seconds up to maybe 8 to 10. I thought it would be nice to show something while the users are waiting, so that they dont assume the app is stuck.
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
No need to move the Sub.

I assume they are calculations inside For-Next (or Do While/Until) loops. Put a Sleep(30) inside one or more of them (30 is an approximate value).
Note that the Sub containing Sleep becomes a Resumable Sub, so treat it as such, in the way you execute it (using Wait For) and for the data type it returns.



The "progress bar" as you described it is not a progress bar, but something that rotates until the processing is complete. You can find some like that on the site.
Its something like this you describe. However, i feel that adding sleeps in between steps would increase the waiting time unnecessarily, wouldnt it?
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
That's true. You need to call Sleep(0) inside your code. Don't call it every loop.

Do something like:
B4X:
If index Mod 100000 = 0 Then Sleep(0)
I tried something like this, but as soon as the heavy work begins, the indeterminate progress bar stops
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
I optimized the work processes to take a couple of seconds to finish, so that I dont need to show anything.
Thank you anyway!! :)
 
Upvote 0
Top