Android Question How to replace this routine using resumable subs

LucaMs

Expert
Licensed User
Longtime User
Some members uses routines like:

B4X:
' call
Wait(1000)

' routine
Sub Wait(Duration As Long)
    Dim ExitTime As Long = DateTime.Now + Duration
    Do While DateTime.Now < ExitTime
        DoEvents
    Loop
End Sub
It's purpose is to pause the app and refresh the GUI.

I tried to create a similar "function" to get the same behavior but avoiding the deprecated DoEvents.

This is my last attempt:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layMain")
   DateTime.TimeFormat = "mm:ss:SSS"
End Sub

Sub Activity_Resume
   For i = 1 To 3
     LogWithTime("I : " & i, Colors.Red)
     Show
   Next
End Sub

Sub Show
   For j = 98 To 100
     LogWithTime("J : " & j, Colors.Blue)
     Label1.Text = j
     Wait(1000) ' here I wish wait for one second, with GUI refresh
   Next
End Sub

' ******************************************************
' ***  These two routines should do what "I" need.
' ***  Putting them in a simple library, you could have
' ***  a Wait command which does what I mean.
Public Sub Wait(Duration As Long)
   LogWithTime("Sub Wait", Colors.Black)
   WaitHelper(Duration)
   Wait For WaitHelper_Completed
End Sub

Private Sub WaitHelper(Duration As Long)
   LogWithTime("Sub WaitHelper", Colors.Black)
   Sleep(Duration)
   CallSubDelayed(Me, "WaitHelper_Completed")
End Sub
' ******************************************************

Sub LogWithTime(Text As String, Color As Int)
   LogColor(DateTime.Time(DateTime.Now), Colors.Green)
   LogColor(TAB & Text, Color)
End Sub

but it does not work as expected, because the GUI is not refreshed and, strangely, the 1 second pauses do not happen.


Is there a way to create a similar (same behavior) routine using resumable subs and avoiding to use DoEvents?

Thank you
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot achieve the exact same behavior. Such DoEvents loops never work properly. Only some of the events are raised and is some cases they can lead to crashes.

As a general rule you should use Sleep to pause the current sub and resume it later (the code you posted is incorrect and not required). This allows the UI to be updated.

As I wrote in the other thread it would have been simpler to remove the Show method and move the code to Activity_Resume:
B4X:
Sub Activity_Resume
   For i = 1 To 3
     LogColor("I : " & i, Colors.Red)
     For j = 98 To 100
       LogColor("J : " & j, Colors.Blue)
       Label1.Text = j
       Sleep(1000) '<--------
     Next
   Next
End Sub

The other option is to call Show and then wait for it to complete:
B4X:
Sub Activity_Resume
   For i = 1 To 3
     LogColor("I : " & i, Colors.Red)
     Show
     Wait For ShowComplete
   Next
End Sub

Sub Show
   For j = 98 To 100
     LogColor("J : " & j, Colors.Blue)
     Label1.Text = j
     Sleep(1000) '<------
   Next
   CallSubDelayed(Me, "ShowComplete")
End Sub
 
Upvote 0
Top