Android Question What's the preferred way to do a wait?

Troberg

Well-Known Member
Licensed User
Longtime User
Occasionally, especially in some GUI cases, one wants to wait a short while before continuing. For example:

Do something
Show a message
Wait 3 seconds
Show another message
Wait 3 seconds
Clear messages

In this case, it's messages, but it's not limited to that.

What is the preferred method om implementing such a wait. Sure, I could use a timer, but that would either lead to a very awkward partitioning of the code (especially if large piles of variables are needed both before and after the wait) or to the use of a DoEvents-loop, which will drain the battery.

So, what is the proper way of doing this?
 

Informatix

Expert
Licensed User
Longtime User
Occasionally, especially in some GUI cases, one wants to wait a short while before continuing. For example:

Do something
Show a message
Wait 3 seconds
Show another message
Wait 3 seconds
Clear messages

In this case, it's messages, but it's not limited to that.

What is the preferred method om implementing such a wait. Sure, I could use a timer, but that would either lead to a very awkward partitioning of the code (especially if large piles of variables are needed both before and after the wait) or to the use of a DoEvents-loop, which will drain the battery.

So, what is the proper way of doing this?
Personally, I would use the Threading library. First, because it's what Google recommends (in their Google Play Services library, for example, all disk and network operations are done in the background). Second, because the timer solution is just horrible for me: reading a flag constantly to know its state instead of raising an event (calling another function) when this state changes is something that I would avoid at all costs in an event-driven language (except for semaphores, but the use of a semaphore means we are using background threads).

You don't have to worry about passing your data from a function to another. Only the reference is copied, not the contents (except for primitive types, but I suppose that your data are in an array, a list, a map or something like that).
 
Upvote 0
D

Deleted member 103

Guest
Personally, I would use the Threading library. First, because it's what Google recommends (in their Google Play Services library, for example, all disk and network operations are done in the background). Second, because the timer solution is just horrible for me: reading a flag constantly to know its state instead of raising an event (calling another function) when this state changes is something that I would avoid at all costs in an event-driven language (except for semaphores, but the use of a semaphore means we are using background threads).
That is, according to my opinion, also the best away.
B4X:
Dim thread1 As Thread
thread1.Initialise("")
thread1.Sleep(1000)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Here is an example of unpredictable code. If I use this routine in the Activity_Create sub to create a delay (to hold a logo on the screen for 2 seconds), everything is fine:

B4X:
    If FirstTime Then             
      WTicks1 = DateTime.Now
      Do While WTicks2 < (WTicks1+2000)
        WTicks2 = DateTime.Now
        DoEvents
      Loop
End If

But if I change this under Activity_Create by:
B4X:
WACHT1(2000)
and:
B4X:
Sub WACHT1(Duur1 As Long)           'Duur1 in msec!
    WTicks1 = DateTime.Now
    Do While WTicks2 < (WTicks1+Duur1)
      WTicks2 = DateTime.Now
      DoEvents
    Loop
End Sub
then strangely enough a ListView will not open later on in my app when I press the button that holds the Listview1.visible = True code. Unexplainable! It must have something to do with the use of DoEvents. Any thoughts?
Anything producing an event or handling events should be out of Activity_Create. It's definitely not the place for that. Activity_Create is meant to create classes instances, initialize your variables and do other preparatory works. The activity will start interacting with the user in Activity_Resume. For the same reasons, Activity_Create should not be the place for disk or network operations (that being said, it is safe to access the assets or open a database in Activity_Create).
Note that's how Google made things, it's not due to B4A.
 
Upvote 0
Top