Wait n seconds

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are not allowed to pause the main thread as it makes your application unresponsive. Android kills blocked applications after 5 seconds, showing "Application not responding".

In almost all cases the best solution is to start a timer and then continue the execution from the Timer_Tick event. First disable the timer so it will not tick again.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
You are not allowed to pause the main thread".

At times, it makes sense to simulate a "program is working" state just for some milliseconds, when interacting with the user.

In these cases i use the following function, which seems to work fine:

B4X:
Sub Sleep(ms As Long)
Dim now As Long
   If ms > 1000 Then ms =1000   'avoid application not responding error
   now=DateTime.Now
   Do Until (DateTime.Now>now+ms)
     DoEvents
   Loop
End Sub


Is this bad programming style?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
At times, it makes sense to simulate a "program is working" state just for some milliseconds, when interacting with the user.

In these cases i use the following function, which seems to work fine:

B4X:
Sub Sleep(ms As Long)
Dim now As Long
   If ms > 1000 Then ms =1000   'avoid application not responding error
   now=DateTime.Now
   Do Until (DateTime.Now>now+ms)
     DoEvents
   Loop
End Sub


Is this bad programming style?

Yes. Bad practice.
Source: (http://www.b4x.com/forum/basic4android-updates-questions/11167-forcing-finish-startactivity-2.html)
DoEvents probably doesnt let the message que process very far. And then you are tying up processor and thread resources.
If another event in your thread fires, I wonder what will happen?
Also, if you need to start a service, and then your wait like this, I wonder when it can be started?
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Yes. Bad practice.

Would you consider the msgbox function as bad programming practice, too?

msgbox has the same effect on system resources as my sleep function. msgbox does basically the same: it uses DoEvents.

I would agree with you, if the sleep Function would stop program execution for several seconds. But we're talking about some milliseconds.
The duration of the loop is in a range of 1 to 1000 milliseconds.

Setting up a timer and a timer event would need system resources, too.
And it is far more complicated to handle. I prefer a single-liner like

B4X:
sleep(100)

Correct me if i am wrong, but i haven't yet experienced any backdraws or side effects of this function.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Erel has taken great pains for implementing MsgBox.
For e.g. you cannot start a service and then show a msgbox
http://www.b4x.com/forum/basic4android-updates-questions/11201-code-activity_create-not-running.html

You cannot show a MsgBox in activity_pause etc...there are a lot of workarounds.

I think this method is more resource, process and battery heavy. Timers work through the message que so they will not lock up threads.
I know it is easier to do a one-liner and it doesnt look that bad (it might not be).

But again it depends on your design. I am just still wondering what will happen if another event fires somewhere? Where does the thread go?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Msgbox is a bit different than DoEvents. DoEvents only processes UI related messages, while Msgbox deals with all messages and is more "sophisticated". Msgbox was designed to block code execution for long durations.

The original question was "how to pause the program for x seconds". I think that if you have a good reason to pause it for 100 milliseconds then such a "busy wait" loop is a valid solution. I do recommend you however to remove DoEvents unless you want to update the layout.
 
Upvote 0

NFOBoy

Active Member
Licensed User
Longtime User
I want to follow up on this, as both methods look like something I will try.

I want to use SoundPool to load up about 30 short audio clips. Then based on a random number generated (0-100000), parse out the number to grab the audio files needed to "pronounce" the number.

(e.g. 4836 would use audio clips "four.wav" + "thousand.wav" + "eight.wave" + "hundred.wav" + "thirty.wav" + "six.wav")

From what I can see, I don't see a way to play the files linearly, so I plan on using a "wait" function synced to the length of a particular segment, and then call the play event for the next sound clip.

Looking at resources, in order for my idead to work well, needs to be less than a few milliseconds in between each audio clip starting... so was going to go with either of these methods.
Anyone with experience on best way to approach?

Ross
 
Upvote 0

NFOBoy

Active Member
Licensed User
Longtime User
Using TTS

Hi Klaus,

I did give TTS a try... and managed to get the Vaja TTS module installed... but it only does Thai/English. (wanting to do more than just those two, so hit a wall there)

I have yet to get the .setLanguage Call to work to even change the default language.. and don't see a way with B4A to call up the available languages.. so hit a wall there.

Also, as my app will be more than just the numbers, as it appears I have to go down the voice clips route for other things, would be nice if the voice(s) matches the other parts. (not a wall, just a different route)

Admittedly the TTS works well for numbers, but it breaks down on individual characters from the alphabet(s). (again, not a wall, but a route)

So, back to my numbers idea... any ideas on how to play audio clips linearly from separate audio files... while trying not to be a "bad coder" ? :) (although my code is always on the wrong side of the tracks)

Ross
 
Upvote 0
Top