B4J Tutorial [B4X] Resumable Subs - Sleep / Wait For

Status
Not open for further replies.

Gary Miyakawa

Active Member
Licensed User
Longtime User
Yes, SQL and http for example are not related to ui

This is my testing code and it definitely doesn't "sleep" for 30 seconds.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
  
End Sub

Sub AppStart (Args() As String)

    Log(DateTime.Now)
    hold(30000)
    Log(DateTime.now)
    StartMessageLoop
End Sub

Sub hold(time As Int)
  
    Sleep (time)
  
End Sub

Thoughts ? BTW, I'm running this in a RPI...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is async.
That means that between the 2 datetime.now will not be any delay.
That's 100% correct.

Quoting the tutorial:
Whenever Sleep or Wait For are called, the current sub is paused. This is equivalent to calling Return.

Only the resumable sub itself is paused and resumed.
Correct code:
B4X:
Sub AppStart (Args() As String)
    hold(30000)
    StartMessageLoop
End Sub

Sub hold(time As Int)
    Log(DateTime.Now)
    Sleep (time)
   Log(DateTime.now)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: As explained in the first post, when one sub calls a second resumable sub, the code in the first sub will continue after the first Sleep or Wait For call (in the second sub).

If you want to wait for the second sub to complete then you can raise an event from the second sub and wait for it in the first:
B4X:
Sub FirstSub
   Log("FirstSub started")
   SecondSub
   Wait For SecondSub_Complete
   Log("FirstSub completed")
End Sub

Sub SecondSub
   Log("SecondSub started")
   Sleep(1000)
   Log("SecondSub completed")
   CallSubDelayed(Me, "SecondSub_Complete")
End Sub

Logs:
FirstSub started
SecondSub started
SecondSub completed
FirstSub completed

Notes:
- It is safer to use CallSubDelayed than CallSub. CallSub will fail if the second sub is never paused (for example if the sleep is only called based on some condition).
- There is an assumption here that FirstSub will not be called again until it is completed.
- In B4J v5.50 beta #2 there will be a warning about a missing sub. This is fixed for the next update.
 

Kirby Leeper

Member
Licensed User
Longtime User
Just fansastic work. I'm glad I found B4X.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for this great feature.
Build a small test app with a Progressbar Class which is called by the Main Class via timer.
As this is a new feature, pls share if there are other / better ways to show an updating progressbar in a class.

EDIT: Removed Wait For and added Sleep (based on guidance Post #28).

Main Class Snippet
B4X:
Sub Process_Globals
    Public tmr As Timer
    Public tmrinterval As Long = 500
    Private ProgressValue As Int = 0
    Private ProgressDlg As ProgressDialog
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    ProgressDlg.Initialize(True)
    tmr.Initialize("tmr", tmrinterval)
    tmr.Enabled = False
End Sub

Sub tmr_Tick
    ProgressValue = ProgressValue + 10
    ProgressDlg.ProgressUpdate(ProgressValue)
    Sleep(0)
    If ProgressValue = 100 Then tmr.Enabled = False
End Sub

ProgressBar Class Snippet
B4X:
Sub Class_Globals
    Private fx As JFX
    Private frm As Form
    Private ProgressBar1 As ProgressBar
    Private btnClose As Button
    Private mCloseAtCompletion As Boolean = False
End Sub

Public Sub Initialize(CloseAtCompletion As Boolean)
    frm.Initialize("frm", 200, 100)
    frm.SetFormStyle("UNDECORATED")
    frm.Resizable = False
    frm.RootPane.LoadLayout("ProgressDialog")
    ProgressBar1.Progress = 0
    mCloseAtCompletion = CloseAtCompletion
    btnClose.Visible = Not(mCloseAtCompletion)
End Sub

'Update the progress bar with a value between 0 and 100
public Sub ProgressUpdate(value As Double)
    frm.show
    ProgressBar1.Progress = Round(value) / 100
    If mCloseAtCompletion And ProgressBar1.Progress = 1 Then frm.close
End Sub

Sub btnClose_Action
    frm.close
End Sub

 

Attachments

  • waitforprogressbar.zip
    3.7 KB · Views: 769
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't think that you are using Wait For correctly. It is not needed in the code you posted.

Example of using Sleep to update the progress bar inside a long loop:
B4X:
Sub SubThatDoesAVeryLongCalculation
   Dim mx As Long = 100000000
   Dim i As Long
   For i = 1 To mx
     If i Mod 10000 = 0 Then
       ProgressDlg.ProgressUpdate(i * 100 / mx)
       Sleep(0)
     End If
   Next
End Sub

Note that I made a few changes to your project. See the attachment.
 

Attachments

  • SleepWithProgress.zip
    3.7 KB · Views: 806

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for clarity = gives a better understanding when to use Wait For and Sleep.
For the test app I made, by keeping on using a timer, have removed the Wait For (in AppStart) and added Sleep(0) to the tmr_Tick after Progress Update, which works fine.
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Can not wait to get the Release.
Thanks Erel
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Hello!

This work:

B4X:
            CallSubDelayed(Main, "initSQL_Complete")

    wait for initSQL_Complete 'in Main

But triggers an IDE warning for Sub initSQL_Complete not found!
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I have a question regarding duck typing.

i am working on a small example where i am lazy enough to write a code several times:

B4X:
    'Panels, tableviews, radioButtons.
    Dim n() As Node = Array As Node (Pane1,twPerfiles,twEmpleos,rdbSubTabla0,rdbSubTabla1)
   
    For Each n2 As Node In n
        n2.Alpha = 0
        n2.visible = True
        n2.SetAlphaAnimated(750,1)
       
        wait for (n2) pane1_AnimationCompleted
       
    Next

It will run for pane1 only, after that it will just wait eternally for twPerfiles, because the name of the event is not the same.
will it be possible to run this event with duck Typing?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
after that it will just wait eternally for twPerfiles
Note that the program will not actively wait for the event. It is similar to having a sub that is never called.

You will need to set the EventName parameter of all the nodes to be the same.

Or use Sleep(750) instead of waiting for the event.
 

jahswant

Well-Known Member
Licensed User
Longtime User
This can never be supported as the code execution returns when Sleep or Wait For are called. The sub is later resumed. Long after the calling sub has already completed.
I understand now.
 

leitor79

Active Member
Licensed User
Longtime User
Great Feature, thank you very much!

I have a question; I don't get why the output of the code quoted here is different than the output in this gif:




Regards,

 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
because of the wait for here

B4X:
 Wait For SecondSub_Complete

without this line, sub1 will continue even when sub2 has not finished.

this line is telling the code, wait until sub2 is finished to continue with sub1.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…