New feature: Resumable Subs - simpler asynchronous programming

ilan

Expert
Licensed User
Longtime User
this is really amazing @Erel.
i have 1 question please. what will happen if you start that action (like in post #40)
and you exit the app before all 5 calls has finished. then you return to the app. will they continue from where it stopped when you exit the app? something like a timer?
 

Widget

Well-Known Member
Licensed User
Longtime User
Can there be more than one Sleep() loop running at the same time?
In other words, can they be nested?

In this example MySubB has a Sleep() loop that calls MySubC that has its own Sleep() loop.

TIA

B4X:
sub MySubC(aCount as Int)
  DoWhile i < aCount
     ..do something..
     Sleep(400)
     i = i + 1
  Loop
End Sub

sub MySubB
  DoWhile i < count
      ... do something ...
     Sleep(400)
     
     MySubC(200)
     ... do something ...
     Sleep(400)
     i = i + 1
  Loop
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
and you exit the app before all 5 calls has finished. then you return to the app
In B4J (and B4i) the app is always running until it is killed. It is never paused. The resumable subs will be killed together with the process.
In B4A (Android) there are some nuisances related to the activity life cycle.

Can there be more than one Sleep() loop running at the same time?
Yes. You can see in the countdown example that there is a different resumable sub created with each click.

In other words, can they be nested?
Yes.
 

imbault

Well-Known Member
Licensed User
Longtime User
This feature looks like a small new one but it's a new super powerful feature

Congrats Erel for this
 

moster67

Expert
Licensed User
Longtime User
Agree, I was looking over the Android-documentation but could not find it unless I missed it. Maybe it will be a B4A exclusive,
It is really more amazing Erel can implement it also for B4i (iOS) and B4J
He never stops to surprise me!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This feature looks like a small new one but it's a new super powerful feature
That's true. It is the most important programming language improvement since the addition of classes in B4A v2.0.

Agree, I was looking over the Android-documentation but could not find it unless I missed
It is not related to the OS. It is a compiler feature.
 
D

Deleted member 103

Guest
There is no relation between different calls to PrintNumber.
I did not understand. :(
Is the sequence then fixed as with random generator?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   PrintNumber(3)
   PrintNumber(2)
   PrintNumber(1)
   Log("After")
End Sub

Sub PrintNumber(i As Int)
   Sleep(i) '<--------------
   Log(i)
End Sub
Each call of PrintNumber is not dependent on other calls.
The first call will print 3 after 3ms. The second will print 2 after 2ms and the third will print 1 after 1ms.
They are called one after another immediately, so the output will be:
1
2
3
 

moster67

Expert
Licensed User
Longtime User
So if you delayed the first one from 3 to 10 the new order will be
2
3
1

??
 

JohnC

Expert
Licensed User
Longtime User
Excellent!
 

bigbadfred

Member
Licensed User
Longtime User
I Love it! Will be useful during downloads of xml files, avoiding spaghetti code without those callback subs.
 

Diego Roman

Member
Licensed User
Thank you. Is a powerful improvement and Sleep function is very useful. :)
Since I know B4J I love it!. Is more simple than other languajes and more fast create really cool and useful aplications and process. Please continue making improvement to B4J.;) and please avoid to make the code so difficult to understand and write, keep it easy.:rolleyes:
 
Last edited:

Martin Larsen

Active Member
Licensed User
Longtime User
This feature is similar to stackless coroutines or async / await in other programming languages.

It is not similar to a loop with DoEvents as the main thread is not caught in the sub. The sub context is stored and is later resumed.

This is a very useful feature. I guess that if you need to stop the resumable sub you simply break out of the while loops. Is that correct?
 

billzhan

Active Member
Licensed User
Longtime User
We can use this feature for B4J server handler and websocket handler, am I correct?

Will it be possible to implement timeout?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
We can use this feature for B4J server handler and websocket handler, am I correct?
Probably yes.

Will it be possible to implement timeout?
The timeout should be implemented in the code that raises the event. Nothing bad will happen if you wait for an event and it is never raised. The code will not block.
 
Top