B4J Question Force GUI to update before long calculations

cyiwin

Active Member
Licensed User
Longtime User
Hello,

I'm trying to display a message to indicate the computer is "working" before a 20 second calculation starts. Here is my sub:

B4X:
Sub btn_Rolling_Apply_0_Action
    btn_Rolling_Apply(0).Text = "Working..."
    CallSubDelayed(Me, Rolling_Average(0))
End Sub

CallSubDelayed isn't waiting for the btn text to change to "Working...".

I've also tried:

B4X:
Sub btn_Rolling_Apply_0_Action
    btn_Rolling_Apply(0).Text = "Working..."
    Do Until btn_Rolling_Apply(0).Text = "Working..."
    Loop
    CallSubDelayed(Me, Rolling_Average(0))
End Sub

but no go. How do I get the program to wait for the gui to update before going to that sub?

I've tried the threading library and it breaks in debug mode (which I use almost exclusively).
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Running your long calculations off the main thread is the preferred method. If you don't want to use the Threading library, try using CallSubExtended.

If your calculation takes 20 seconds to complete, what's another second? Try using a Timer to delay the start of your calculation by one second:
B4X:
Sub btn_Rolling_Apply_0_Action
    btn_Rolling_Apply(0).Text = "Working..."
    Dim t as Timer
    t.Initialize("t", 1000)
    t.Enabled = True
End Sub

Sub t_Tick
    Dim t as Timer = Sender
    t.Enabled = False
    CallSubDelayed(Me, Rolling_Average(0))
End Sub

You can probably get away with using a delay of less than 1 second; experimentation is your friend.
 
Upvote 0

cyiwin

Active Member
Licensed User
Longtime User
That's great, thanks! For me a 10 ms delay works mostly, 50 to be safe :)
 
Last edited:
Upvote 0
Top