B4R Question Question about CallSubPlus.

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
I love this statement! This feature saved me, because it allowed simple parallel operations in a complex program of mine without the use of multitasking software.
I'm wondering if the Call is queued. To better explain:
B4X:
CallSubPlus("Routine1",10,0)
CallSubPlus("Routine2",20,0)
CallSubPlus("Routine3",30,0)
CallSubPlus("Routine4",40,0)
return
In this case execution of the four routines is queued or only the last/first is executed?
Thanks Anywhere Software for this very useful package.
Mauro Zanin
 

tigrot

Well-Known Member
Licensed User
Longtime User
Test:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    CallSubPlus("S1",1000,0)
    Log("D1")
    CallSubPlus("S2",1000,0)
    Log("D2")
    CallSubPlus("S3",1000,0)
    Log("D3")
    CallSubPlus("S4",1000,0)
    Log("D4")
    CallSubPlus("S5",1000,0)
    Log("D5")
End Sub
Sub s1
    Log("s1",Millis)
End Sub

Sub s2
    Log("s2",Millis)
End Sub

Sub s3
    Log("s3",Millis)
End Sub

Sub s4
    Log("s4",Millis)
End Sub

Sub s5
    Log("s5",Millis)
End Sub

Result in log
B4X:
AppStart
D1
D2
D3
D4
D5
s11000
s41001
s31001
s21002
s51003

the result is: THE calls are queued not in native order but are queued(at least up to 5!)
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
In my case I need to "homemade-multitask" Wait for an event while keep on serving other events. I have a data logger on SD, able to copy the SD content on a USB flash memory simply inserting the memory. In the meanwhile it allows to set-up RTC.
If memory is non in, data from a metal detector(an old project of mine) are stored in textual fashion on a SD, while it handles LCD and buttons remotelly, doubled from the metal detector.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
I think that the casual order of execution is because they have all the same delay. When time comes the first tested in the queue is the the first called, till they are called all. Putting delay in cascade 1000- 1003- 1006 and so on, should solve. It's a real queue or it's a list of linked elements?
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
A real queue in my opinion is a list in which you can add an element and subtract in fifo manner without a way to operate on middle elements, a linked list allows you to operate on every element, simply changing links between elements. I used this to write an isam file handling for IBM1 in 1980, because it had only physical record handling and no isam.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is implemented with a linked list as you can add elements with different intervals.

Change your code to:
B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    CallSubPlus("S1",1000,0)
    Log("D1")
    CallSubPlus("S2",1001,0)
    Log("D2")
    CallSubPlus("S3",1002,0)
    Log("D3")
    CallSubPlus("S4",1003,0)
    Log("D4")
    CallSubPlus("S5",1004,0)
    Log("D5")
End Sub
The order will be kept (I haven't tried it).

My guess is that the order wasn't kept because the code executed in less than a millisecond.
 
Upvote 0
Top