Sleep Pause Wait System Timer

SlavaVB

Member
Licensed User
Longtime User
whats the best way to create a delay? I tried: sleep, wait, pause .. and dont know how to access the system timer variable (in VB its just, timer... but in b4a its reserved for the timer object)

thanks,
Slava

:BangHead:
 

SlavaVB

Member
Licensed User
Longtime User
never mind, thanks,... i found the following code...

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



How would you go about making a moorse code app without sleep? you would have to create some kind of global variable or something and then track it... i would rather parse the text character by character and then generating the pulses using sleep()
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
B4X:
Dim Timer1 as Timer( in Process Globals)

Timer1.Intitialize("Pause",2500)
Timer1.Enabled=True


Sub Pause_Tick
Timer1.Enabled=False
'Now continue to another sub...As far as I know, it is not possible to return to a label in B4A..yet)
End Sub
 
Upvote 0

Amalkotey

Active Member
Licensed User
Longtime User
My subrotine "Wait"

@SlavaVB:

My subroutine Wait wait x seconds:

B4X:
Sub Wait(Sekunden As Int)
   Dim Ti As Long
   Ti = DateTime.Now + (Sekunden * 1000)
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub

by calling: Wait ( seconds )

regards
Amalkotey
 
Upvote 0

JOANORSKY

Member
Licensed User
Longtime User
Sorry about digging an old post.. but.. is it there already any b4a function that sets a delay for more than 5 seconds?

I've been reading.. and reading.. and reading.. but found nothing except variations of this method.

I am asking this.. because i need delays for up to 1 minute for sending repeatable SMS.. and i am not quite sure on how to do these tasks with a timer (if possible at all.. well.. maybe.. i don't know).

If i use the above functions.. the Android OS will shut me down for intervals bigger than 5 seconds..

Imagine like this..

1) I need to send X amount of messages to a destination
2) Each message must be separated by Y amount of seconds (up to 1 minute)

.. and i did it like this following this pseudo code:

for i = 1 to X
send message_to_destination
wait Y seconds
next


BUT if Y>5000ms (or close).. the OS cracks the app up ... so this is not a viable solution for delays.

Although i have some understanding of how programming languages do work.. on B4A i am still a newbie.. so.. have some patience with me if i am not making any sense on my questions.. eheh
 
Upvote 0

MotoMusher

Active Member
Licensed User
Longtime User
The while loop crushes the battery and makes the app go ANR. You don't want to use it for pauses, ever.

Use the code from cableguy above. Variable passed is in MilliSeconds, so 5000 is 5 seconds, 10000 is 10 seconds etc.

in Process_Globals
Dim Timer1 As Timer

Split your current sub

Sub FirstHalf
do stuff
Timer1.Intitialize("Pause",2500) '2.5 seconds
Timer1.Enabled=True

end sub

sub SecondHalf
do more stuff
end sub


Sub Pause_Tick
Timer1.Enabled=False
SecondHalf
End Sub
 
Upvote 0
G

GCOINC

Guest
I needed the 'pause' for SMS as well.

After you initialize the timer on create
B4X:
Timer1.Initialize("Timer1", 1000) ' 1000 = 1 second
and after the first message is sent via the Sub Timer1_Tick() Sub.

I call the below 'pause' in that Sub Timer1_Tick() just below where the message is sent -
B4X:
Timer1.Interval = Rnd(46000,88000)

With my provider only 100 messages per hour can be sent, which is every 46 seconds roughly - So I randomized the 'pause' -
 
Last edited by a moderator:
Upvote 0

JOANORSKY

Member
Licensed User
Longtime User
The while loop crushes the battery and makes the app go ANR. You don't want to use it for pauses, ever.

Use the code from cableguy above. Variable passed is in MilliSeconds, so 5000 is 5 seconds, 10000 is 10 seconds etc.

in Process_Globals
Dim Timer1 As Timer

Split your current sub

Sub FirstHalf
do stuff
Timer1.Intitialize("Pause",2500) '2.5 seconds
Timer1.Enabled=True

end sub

sub SecondHalf
do more stuff
end sub


Sub Pause_Tick
Timer1.Enabled=False
SecondHalf
End Sub

Well .. i understand your meaning.. but this doesn't quite solve the problem. Let me explain why..

You see... if you use a FOR cycle in the main sub in order to make multiple operations (the sub to split), AND IF you do require a delay from within such a Cycle.. then.. the timer itself will always being initialized until the end of the cycle.. and then.. it runs. Making not the whole delays happen.. but just the last one.

Hum.. how can i say this?:BangHead:

By your code concept... how can i use a sub split to implement this timer idea :

--------------------
Main Sub
--------------------
some_previous_code_here

For i = 1 to 200
wait_2000_ms
do_some_other_job
next

some_following_code_here
--------------------
 
Upvote 0

MotoMusher

Active Member
Licensed User
Longtime User
I wouldn't do a for loop, I'd do a counter.

In process_globals:
Dim i as int = 1

B4X:
Sub FirstHalf()
    dostuff with i
     set your timer
end sub

Sub SecondHalf()
   domorestuff with i
   If i <200 then
       i = i + 1
       FirstHalf
   end if
end sub

If you don't need to domorestuff with i, then just put the if statement in SecondHalf in the timer_tick event and you won't need SecondHalf sub at all

B4X:
Sub FirstHalf()
    dostuff with i
    Timer1.Intitialize("Pause",2500) '2.5 seconds
    Timer1.Enabled=True
end sub

Sub Pause_Tick
   Timer1.Enabled=False
   If i <200 then
       i = i + 1
       FirstHalf
   end if

End Sub
 
Last edited:
Upvote 0

ashrafidkaidek

Member
Licensed User
Longtime User
Sub Wait(Sekunden As Int) works very good, but the minimum wait time is 1 second. Is there a way to pause for a fracture of a second?

Thank you all
 
Upvote 0

Amalkotey

Active Member
Licensed User
Longtime User
Sub Wait(Sekunden As Int) works very good, but the minimum wait time is 1 second. Is there a way to pause for a fracture of a second?

Thank you all

One second is 1000 milliseconds. Here the sub based on milliseconds.

B4X:
Sub Wait(MilliSekunden As Int)
   Dim Ti As Long
   Ti = DateTime.Now + MilliSekunden
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
One second is 1000 milliseconds. Here the sub based on milliseconds.

B4X:
Sub Wait(MilliSekunden As Int)
   Dim Ti As Long
   Ti = DateTime.Now + MilliSekunden
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub


i had tested your code on my device it made the cpu usage 100% all the time until the loop ends
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
As in another post .... DoEvents is a no-no in Android 4.4 !

It causes app to behave erratically as processes start/end at non-user controlled times.

Avoid at all cost ... use Tick event.
 
Upvote 0
Top