B4J Question Doubt about resumablesub

Status
Not open for further replies.

jroriz

Active Member
Licensed User
Longtime User
Hi.

Let's say I have a sub like this:

B4X:
Sub Process
    Sleep(0)
    ''' long process
End Sub

What's the difference in calling the function like this
Process

or that
Wait For (Process) complete (r As Object)

since the function does not return values.
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thats my point. Notice that sleep(0) is the first sentence in the sub.
Still the waitfor will wait for the process to finish, even with no return?
Yes...
Sleep(0) does not pause the rest of the sub code to be executed... While the wait for will do just that, wait for all of the code on the colled sub to be executed, and only then continue...
The fact that the called sub has no return value is not important since after that last codeline of the called sub is executed, the flow returns to the caller sub.
Furthermore, wait for is not wait forever... If that called sub takes more than expected to be executed, the wait for will be ignored and the sub will continue...
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Hi.

Let's say I have a sub like this:

B4X:
Sub Process
    Sleep(0)
    ''' long process
End Sub

What's the difference in calling the function like this
Process

or that
Wait For (Process) complete (r As Object)

since the function does not return values.
Since the sub returns no value, you don't need the rest of the declaration
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Yes...
Sleep(0) does not pause the rest of the sub code to be executed... While the wait for will do just that, wait for all of the code on the colled sub to be executed, and only then continue...
The fact that the called sub has no return value is not important since after that last codeline of the called sub is executed, the flow returns to the caller sub.
Furthermore, wait for is not wait forever... If that called sub takes more than expected to be executed, the wait for will be ignored and the sub will continue...
The waitfor will wait the end of the sub, despite the sleep there? I mean, sleep means return, but its not true for waitfor? Thats my point.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Sleep does not mean return, it means "pause for X milliseconds".
Sleep (0) is used as an alternative to DoEvents, which is deprecated, and it's purpose is to allow the OS to not block the UI
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Whenever Sleep or Wait For are called, the current sub is paused. This is equivalent to calling Return.
Can you show me a code example where a sub "returns" due to a wait for or a sleep (0)?

I have never come across a post or code showing this behaviour...
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
From the "wait for / sleep" tutorial, we can read...

"
Sleep

Using Sleep is simple:
Code:
Log(1)
Sleep(1000)
Log(2)
The sub will be paused for 1000 milliseconds and then be resumed.

You can call Sleep(0) for the shortest pause. This can be used to allow the UI to be refreshed. It is a good alternative to DoEvents (which doesn't exist in B4J and B4i and should be avoided in B4A)."
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
"Whenever Sleep or Wait For are called, the current sub is paused. This is equivalent to calling Return."

Yes, this sentence is in fact there... But to me at least, it makes no sense ...

This means that if we call an heavy sub that also does UI stuff with waitfor, we cannot use sleep (0) to allow for UI refresh(!?)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
May I interfere :D
I want to advice you experts (who owns a computer ;)) to create small examples in source code that we can play around with.
For example I studied an example by Cableguy with the use of Animation, so such examples with just two-three step in sequence will be extremely useful.
After all a working example kinda fits like a glove versus only talking. Documented and working nice and tidy commented code is the way of the Jedi programmer,
never forget I said it first haha :cool:
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Yes, it makes diference using waitfor or not.
Wait for ignores the return caused by sleep.

I'm using such code because my app needs to do a lot of things related to file, for later use. This way, the process will be in background.

I put sleep as the first sentence because I WANT the code to return.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
 
    Log("Call Whithout wait for")
    process(1)
    Log("End call Whithout wait for")
 
    Log("Call Whit wait for")
    Wait For (process(2)) complete (r As Object)
    Log("End call Whit wait for")
End Sub

Sub process(i As Int) As ResumableSub
    Sleep(0)
 
    Log("Start process " & i)
    For x = 1 To 50000
        ' Just for the sake of the example.
        ' Yes, yes, I could use delay...
        File.Exists(File.DirApp, "notfound")
    Next
    Log("End process " & i)
 
    Return 0
End Sub

Log:
Call Whithout wait for
End call Whithout wait for
Call Whit wait for
Start process 1
End process 1
Start process 2
End process 2
End call Whit wait for
 
Last edited:
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
May I interfere :D
I want to advice you experts (who owns a computer ;)) to create small examples in source code that we can play around with.
For example I studied an example by Cableguy with the use of Animation, so such examples with just two-three step in sequence will be extremely useful.
After all a working example kinda fits like a glove versus only talking. Documented and working nice and tidy commented code is the way of the Jedi programmer,
never forget I said it first haha :cool:
See above.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Ok, I see... But then, if it is in fact equivalent to return, the rest of that sub will not be executed!
And if all you want is for the sub to return, then do just that: Return
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I'd think of it as a task queue (hope not to be mistaken)
Sleep(...) will in fact return control to the calling sub and other queued tasks, and itself will be queued to resume execution after N (in this case 0) miliseconds.
As it is all executed in the same thread, the result is that the calling sub and pending events will be executed and, as soon as it is allowed, the next enqueued process (the same sub) will continue.
 
Upvote 0
Status
Not open for further replies.
Top