B4J Question Update UI during delay loop

bdunkleysmith

Active Member
Licensed User
Longtime User
I have some code:

B4X:
Sub btnFoulLights_Action        ' Enable foul light testing
    If btnFoulLights.Selected Then
            Dim joRect As JavaObject = Rin1TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))
            Log("1")           
            Wait(2000)   
            Dim joRect As JavaObject = Rin2TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("2")
            Wait(2000)   
            Dim joRect As JavaObject = Rin3TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("3")
            Wait(2000)   
            Dim joRect As JavaObject = Rin4TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("4")
            Wait(2000)   
            Dim joRect As JavaObject = Rin5TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("5")
            Wait(2000)   
            Dim joRect As JavaObject = Vis1TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("6")
            Wait(2000)   
            Dim joRect As JavaObject = Vis2TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("7")
            Wait(2000)   
            Dim joRect As JavaObject = Vis3TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("8")
            Wait(2000)   
            Dim joRect As JavaObject = Vis4TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))           
            Log("9")
            Wait(2000)   
            Dim joRect As JavaObject = Vis5TeamFoulsRect
            joRect.RunMethod("setVisible", Array(True))   
            Log("10")
        Else
            Dim joRect As JavaObject = Rin1TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Rin2TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Rin3TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Rin4TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Rin5TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Vis1TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Vis2TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Vis3TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Vis4TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))           
            Dim joRect As JavaObject = Vis5TeamFoulsRect
            joRect.RunMethod("setVisible", Array(False))   
    End If
End Sub

Sub Wait(ms As Int)
Dim nn As Long = DateTime.Now + ms
Do While DateTime.Now < nn
Loop
End Sub

in which the delay loop is working OK because I can see the number sequence written to the log after each 2 second delay, but each of the rectangles is not made visible in sequence, with all showing together after all delay loops have completed.

How can I make the UI update before each delay loop is called so the rectangles are displayed in sequence?
 

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks Erel, but I'm struggling to see how to use a timer.

Can you please point me to an example that uses a timer for the purpose of inserting a delay?

Thanks,

Bryon
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Run this code:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private timer1 As Timer
   Private nodesToHide As List
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   Dim nodes As List
   nodes.Initialize
   For i = 1 To 10
     Dim b As Button
     b.Initialize("")
     b.Text = i
     MainForm.RootPane.AddNode(b, 40 * i, 50, 30, 30)
     nodes.Add(b)
   Next
   MainForm.Show
   timer1.Initialize("timer1", 2000)
   HideNodesOneByOne(nodes)
End Sub

Sub HideNodesOneByOne (nodes As List)
   timer1.Enabled = True
   nodesToHide = nodes
End Sub

Sub Timer1_Tick
   If nodesToHide.Size = 0 Then
     timer1.Enabled = False
   Else
     Dim n As Node = nodesToHide.Get(0)
     nodesToHide.RemoveAt(0)
     n.SetAlphaAnimated(300, 0)
   End If
End Sub
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks again Erel for your great support. As well as solving the particular problem for me, your use of a list of nodes enabled me to simplify some other code in the application.

What I finished up based on your suggestion is:

B4X:
Sub Process_Globals
.
.
    Private GameClockRect, RinRect, RinScoreRect, Rin1TeamFoulsRect, Rin2TeamFoulsRect, Rin3TeamFoulsRect, Rin4TeamFoulsRect, Rin5TeamFoulsRect,VisRect, VisScoreRect, Vis1TeamFoulsRect, Vis2TeamFoulsRect, Vis3TeamFoulsRect, Vis4TeamFoulsRect, Vis5TeamFoulsRect As Node
    Private foulLights As List
.
.
End Sub

Sub AppStart (Form1 As Form, Args() As String)
.
.
    foulLights.Initialize
    foulLights.AddAll(Array(Rin1TeamFoulsRect, Rin2TeamFoulsRect, Rin3TeamFoulsRect, Rin4TeamFoulsRect, Rin5TeamFoulsRect, Vis1TeamFoulsRect, Vis2TeamFoulsRect, Vis3TeamFoulsRect, Vis4TeamFoulsRect, Vis5TeamFoulsRect))
.
.
    Timer1.Initialize("Timer1", 2000)
.
.
End Sub
.
.
Sub btnFoulLights_Action        ' Enable foul light testing
    If btnFoulLights.Selected Then
            Timer1.Enabled = True
            foulLightsIndex = 0
        Else
            For i = 0 To 9
                Dim nx As Node = foulLights.Get(i)
                nx.Visible = False
            Next
            Timer1.Enabled = False    
    End If
End Sub

Sub Timer1_Tick            ' Lights each foul light in sequence on each tick for testing
    Dim nx As Node = foulLights.Get(foulLightsIndex)
    nx.Visible = True
    foulLightsIndex = foulLightsIndex + 1
    If foulLightsIndex = 10 Then
        Timer1.Enabled = False
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…