Android Question Screen not updating during process

Isa Paine

Member
Licensed User
Here is the scenario. In the item click event of a ListView object I want HideWhileProcessing() to hide one button and make another button visible for 5 seconds. However during my Do While Loop the screen does not update. What is the best way to accomplish this 5 second hiding of a button, then making it visible again once completed?

Sub HideWhileProcessing()
Dim five_seconds As Long = 5 * DateTime.TicksPerSecond
Dim expirationDateTime As Long = DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now),DateTime.GetDayOfMonth(DateTime.Now), DateTime.GetHour(DateTime.Now), DateTime.GetMinute(DateTime.Now), DateTime.GetSecond(DateTime.Now))

expirationDateTime = expirationDateTime + five_seconds

Do While (DateTime.Now < expirationDateTime)
imgRedo.Visible = False
imgProcessing.Visible = True
imgProcessing.Visible = True
Loop

imgRedo.Visible = True
imgProcessing.Visible = False
End Sub
 

sorex

Expert
Licensed User
add DoEvents to your loop so that it gets the time to update the screen.

that approach is bad, you better use a timer instead.
 

sorex

Expert
Licensed User
we are talking about a wait loop here, in this case it's bad.

in other cases like filling a large select list the doevents is a solution to prevent "app not responding" notifications.

but in general wait loops are to be avoided when you have timers available.
 

LucaMs

Expert
Licensed User
we are talking about a wait loop here, in this case it's bad.

in other cases like filling a large select list the doevents is a solution to prevent "app not responding" notifications.

but in general wait loops are to be avoided when you have timers available.

You're almost right.

In this case it is not only a wait like this Erel's routine:
B4X:
Sub WaitFor(Milliseconds As Int)
Dim s As Long
s = DateTime.Now
Do While DateTime.Now < s + Milliseconds
Loop
End Sub
but "wait while something else works" (then Doevents).

This is not "perfect":
B4X:
Do While (DateTime.Now < expirationDateTime)
imgRedo.Visible = False
imgProcessing.Visible = True
imgProcessing.Visible = True
Loop
Better:
B4X:
imgRedo.Visible = False
imgProcessing.Visible = True
imgProcessing.Visible = True
Do While (DateTime.Now < expirationDateTime)
    DoEvents
Loop
Finally, sometimes SomeView.Invalidate is needed and enough.
 

sorex

Expert
Licensed User
in the old days it didn't matter since computers where single process only.

(ok, you could use IRQs and NMIs to do multiple things but for regular programs that was not used.
more for things like music, samples, sprite movements etc since they rely on rasterpositioning or vertical retraces (pc term for the same thing) )
 
Top