B4J Question How to do Substitute of SLEEP(xxx) function

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends, please for advice,

I am using loop
B4X:
Do
callsubtask1
sleep(2000)
callsubtask2
sleep(2000)
callsubtask3
sleep(2000)
Loop

But I am checking if I click on the button. But because I am using SLEEP(2000) function, then after clicking button in app, then Windows is writing something like this: THE APP IS NOT REACTING, app Window is white (disabled), but the program is still running....

Please how can I do substitute od sleep(2000) function?
Thank you very much
p4ppc
 

derez

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim tmr As Timer
    Dim count As Int
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    tmr.Initialize("tmr",2000)
    count = -1
    tmr.Enabled = true
End Sub

Sub tmr_tick
count = (count+1)Mod 3
Select count
    Case 0
        callsubtask1
    Case 1
        callsubtask2
    Case 2
        callsubtask3
End Select
End Sub
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
DEREZ - THANK YOU VERY MUCH, VERY NICE SOLUTION.

Please onlz one question to this theme -

And in next example? How can I substitute each sleep(xxx) order for good running of the app, please very much?

B4X:
sub callsubtask1
' do something
  sleep(2000)
' do something
sleep(1000)
callsubtask2
End sub

sub callsubtask2
' do something
sleep(3000)
' do something
sleep(1500)
call subtask1
End sub

Thank you for your help
p4ppc
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
The following is more general, using 4 timers for the 4 times you have.
Please look at my Multitimer class, it was written with similar case in my mind.
https://www.b4x.com/android/forum/threads/multitimer-class.42610/

B4X:
sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim tmr1,tmr2,tmr3,tmr4 As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    tmr1.Initialize("tmr",2000)
    tmr2.Initialize("tmr",1000)
    tmr3.Initialize("tmr",3000)
    tmr4.Initialize("tmr",1500)

    tmr1.Enabled = False
    tmr2.Enabled = False
    tmr3.Enabled = False
    tmr4.Enabled = False
    tmr4_tick
End Sub

Sub tmr1_tick
tmr1.Enabled = False
' do something
tmr2.Enabled = True
End Sub

Sub tmr2_tick
tmr2.Enabled = False
' do something
tmr3.Enabled = True
End Sub

Sub tmr3_tick
tmr3.Enabled = False
' do something
tmr4.Enabled = True
End Sub

Sub tmr4_tick
tmr4.Enabled = False
' do something
tmr1.Enabled = True
End Sub
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
Simulate SLEEP:

In process_globals definie:
Dim TimerSleep As Timer
Dim TimerContinue As Boolean

And this sub:

private Sub TimerSleep_Tick
TimerSleep.Enabled = False
TimerContinue = True
End Sub


And in your coude instead of sleep, include this code:


TimerContinue = False
TimerSleep.Initialize("TimerSleep", 500)
TimerSleep.Enabled = True
Do While TimerContinue = False
DoEvents
Loop


maybe you can include all code in a module or class.

This code is only an idea, there are not tested.
 
Upvote 0
Top