B4J Question How to use Wait For in subroutine so that subroutine does not return until Wait For fires

JackKirk

Well-Known Member
Licensed User
Longtime User
I have a major AWS EC2 B4J app that processes videos and does a lot of AWS S3 to and froing.

The principal procedure has 16 major steps each one involving a Wait For and currently totals about 5000 lines (including inline documentation) - something like:

======================
Private Sub Principal_sub
...
step1 (contains Wait For)
...
step2 (contains Wait For)
...
...
step16 (contains Wait For)
...
End Sub
======================

Each step must be completed (i.e. its Wait For fired) before the next step can start.

I'm at the point of tidy-up and would like to break each step out into separate Subs - something like:

======================
Private Sub Principal_sub
...
STEP1
...
STEP2
...
...
STEP16
...
End Sub

Private Sub STEP1
...
Wait For ...
...
End Sub

Private Sub STEP2
...
Wait For ...
...
End Sub

...
...

Private Sub STEP16
...
Wait For ...
...
End Sub
======================

This model doesn't work because Sub STEPx returns as soon as its Wait For is reached.

I can see how I could do this by having 1 step call the next - something like:

======================
Private Sub Principal_sub
...
STEP1
...
End Sub

Private Sub STEP1
...
Wait For ...
...
STEP2
End Sub

Private Sub STEP2
...
Wait For ...
...
STEP3
End Sub

...
...


Private Sub STEP16
...
Wait For ...
...
End Sub
======================

but this sort of defeats the purpose of separate subs - i.e to simplify the code structure.

Any thoughts?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
This model doesn't work because Sub STEPx returns as soon as its Wait For is reached.
i think if u calling this u will use also use wait for to get a sequence.

example with a list as return value
B4X:
Wait For (Manager.ListRequest(Filter)) Complete (List1 As List)
For Each Item As ItemType In List1

Sub ListRequest(Filter As String) As ResumableSub
        
    Dim Job As HttpJob
    Job.Initialize("",Me)
    Job.Username = Main.Username
    Job.Password = Main.Password
    Job.Download2("https://" & Main.Server & "/request", Array As String("cmd", "list", "filter", Filter))

    Dim ser As B4XSerializator
    Dim List1 As List
    List1.Initialize
        
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Dim buffer(Job.GetInputStream.BytesAvailable) As Byte
        
        Job.GetInputStream.ReadBytes(buffer, 0, buffer.length)
        List1 = ser.ConvertBytesToObject(buffer)
        'Log(List1.Size)
    End If
    Job.Release
    
    Return List1
    
End Sub
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel, Markus,

Got it - works a treat - many thanks...
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i think the wait for the single steps it not necessary in your case.

B4X:
Wait For (Principal_sub) Complete (Ok As Boolean)

Private Sub Principal_sub As ResumableSub

 STEP1
 STEP2
 ..
 STEP16

 return True
End Sub
 
Upvote 0
Top