Sleep (m/s)

Jim Brown

Active Member
Licensed User
Longtime User
I would like a thread sleep function (which blocks user input) for x milliseconds

B4X:
Sleep(250)

To give you an idea why, I have a Toast message popping up showing a message, followed by the app switching views
At the moment the app will immediately switch views because the toast message is non-blocking

I could use timers as a way to delay the switching although the user would still be able to bash away at the buttons

Is a thread Sleep() function feasible?
 

agraham

Expert
Licensed User
Longtime User
There is a Thread.Sleep method in my Threading library you could use, but like Erel says it should not really be used on the manin GUI thread which should be responsive whenever possible. This will be more important later on when Services and BroadcastReceivers are implemented as they share the main GUI thread with Activities.
 

Jim Brown

Active Member
Licensed User
Longtime User
Thanks for the heads up regarding the possible 'Force Close' situation
I changed my code implementation now to avoid the need for sleeping
 

vangogh

Active Member
Licensed User
Longtime User
I usually use the "sleep" function to show to the user that I am doing something...
When a calculation is too fast (no time), the user often think "mmm... nothing has been done"
in my proggy, at the end of a procedure I put something like "sleep 1 second" with the hourglass cursor shown

the user always says "OH! pretty quick for such that big amount of work DONE, eh!"

that's why a "sleep" sometimes is useful

will use timers, instead :)
 

JohnK

Active Member
Licensed User
Longtime User
I would like a thread sleep function (which blocks user input) for x milliseconds

B4X:
Sleep(250)
...snip...
Is a thread Sleep() function feasible?

There is a Thread.Sleep method in my Threading library you could use, but like Erel says it should not really be used on the main GUI thread which should be responsive whenever possible.

Although I can see and agree that the use of the timer is the better choce, I had some quick animation I had for an app which required a few sleeps of 50ms or less to make it appear smooth, and I originally used a DoEvents loop as people have suggested above, but I just converted to the following, and personally prefer this approach.

As you can see, I am actually using a lock (requires a reference to agraham's threading library, thanks :)), and then blocking the current activity thread for the sleep time using the "WaitFor" on the same lock. As it is used for Animation, the DoEvents was placed there to allow the screen to re-draw, before the actual "Sleep". I also put extra code just in case it wasn't 1000 ticks / seconds on the device.
B4X:
Sub Sleep(ticks As Int)
   DoEvents
   If ticks > 0 AND DateTime.TicksPerSecond > 0 Then
      Dim milliseconds As Int: milliseconds = 1000 / DateTime.TicksPerSecond * ticks
      Dim Lock1 As Lock
      Lock1.Initialize(True)
      Lock1.WaitFor(milliseconds)
      Lock1.Unlock
   End If
End Sub
Anyone see any problems? I tested this sleep function with 10 seconds (ie 10000 milliseconds) and I did not get an "Application is unresponsive. Forceclose, wait?"; of course I wouldn't suggest it for this length of time. Ironically, in the same app I also have a 1 second delay in another code section, where I user the timer method.
 
Last edited:

JohnK

Active Member
Licensed User
Longtime User
In my actual app, the sleep is never called with anything > 50ms, and that I had tested clicking like crazy, with no problems.

I just tried it with the 10 second pause test project, and your right, that gave the ANR message if I clicked during the pause.

I personally prefer the above method for my short times, so will use it in this case, and nothing too much longer. Not a fan of the pure DoEvents loop; even though in practice, the DoEvents at the top of the procedure simulates it as it will happen every 50ms or so (ie between each animation frame).
 
Top