How to Implement a Delay Without the "Application is Not Responding" Error

Fixx42

Member
Licensed User
Longtime User
Hello all,

I have created a timer application that involves delays lasting up to about 10 seconds. When I run the process that involves these delays, I always run up against a "Activity XXXX (in application XXXX) is not responding" error message. Other than reducing the delays that this process incurs (I'm afraid that they're necessary to the app's functionality), does anyone know of a way for me to avoid this error? I would very much appreciate it!

A little more on the app: When the user presses a button, the app plays a sound, waits a few seconds, plays another sound, waits 2 more seconds, plays another sound, and repeats this about 2 more times.

Here's the code I'm using the create the delays:
B4X:
Sub Sleep(ms As Long)
Dim now As Long
    now=DateTime.now
    Do Until (DateTime.now>now+ms)
      DoEvents
    Loop
End Sub

Thank you very much for the help!
 

William Hunter

Active Member
Licensed User
Longtime User
Try this - the call is: Wait(1) - replace the 1 with whatever number of seconds you need.

B4X:
Sub Wait(Seconds As Int)
   'Sleep for defined seconds
   Dim Ti As Long
   Ti = DateTime.Now + (Seconds * 1000)
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub
 
Upvote 0

Fixx42

Member
Licensed User
Longtime User
You should read about TIMER, there are plenty of samples on the forums.

The method you posted is not recommendable, you should use timers.
Thank you very much NJDude, this worked perfectly and required only minor reworking of my existing code. My apologies for not doing a better search job before posting.

Thank you for the input William, but I fear that the code you recommended would also lead to an "unresponsive" error (it is very similar to my existing code).
 
Upvote 0
Top