B4J Question [SOLVED] Sub that became resumablesub

jroriz

Active Member
Licensed User
Longtime User
Well, I have (had) a simple sub that returns a value.

This sub will now use the shell, so it has become a resumablesub.

It so happens that I call these sub from various points in my project, and now each sub that calls this resumablesub will also become a resumablesub, forcing me to also change the sub that called it, and so on recursively...

Is there a way to call a resumablesub without making the procedure that calls it resumablesub too?

I made a small example below to clarify the problem...

B4X:
Sub OldSub As ResumableSub    ' it was just a regular sub before the shell
    
    ' now this sub became resumablesub, because it's calling an old sub that became resumablesub
    ' now, all calls to this sub will become resumablesub, and so on...
    
    Wait For (NewSub) complete (r As String)
    Return  r
    
End Sub

Sub NewSub As ResumableSub    ' it was just a regular sub before the shell
    
    ' this sub was not resumablesub, now it's needed.
    Dim sh As Shell
    sh.Initialize("sh","WHAREVER.EXE",Null)
    sh.Run(-1)
    Wait For sh_ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)

    Return StdOut
End Sub
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
Well you have 2 options
1. Instead of a wait for, create the normal sub and manage the logic of your app from there

2. Call the shell sub and don't use wait for on the calling sub, just use the called sub to manage your logic

It is a bit of a hazzle but that was the way in the old times
 
Upvote 1

Heuristx

Active Member
Licensed User
Longtime User
It is logical that if your Sub waits for something, then it will become a resumable sub itself. It may be puzzling first, because despite of the word "wait", the resumable sub continues the main line of the program immediately(that is, without waiting), but it is worth studying Resumable Subs because they simplify everything when it comes to variable access and eliminate thread locking(!!!). It is crucial to understand Wait For thoroughly. Once you learn what it does, you will appreciate its power. Look at the examples, they may help more than just pondering.

 
Upvote 1

jroriz

Active Member
Licensed User
Longtime User

Thanks for the answers.
1. Instead of a wait for, create the normal sub and manage the logic of your app from there
I don't think it will be possible, because I need the shell, and I need to wait for the result (waitfor). Once you use the shell, you will automatically have a resumablesub.
2. Call the shell sub and don't use wait for on the calling sub, just use the called sub to manage your logic
This would be a huge amount of work, because I would have to move all the logic from the subs that interact with each other to a single sub.
It would be like having a program with a single procedure with no functions...
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
See post number 5
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
See post number 5
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Once you understand how resumable subs work you will understand that this chain of resumable subs is part of the design. There is no way around it (unless you are fine with the code flow continuing without waiting for the sub to complete).
The benefits of resumable subs are huge. They change the way we program.

Simply change the calls to use Wait For. It will not take you more than 5 minutes to do...
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
In fact, what happened was the following:
The project has been around for a few years now, and it's a HUD, which displays poker information on the screen, and it's a pretty complex project.
There was a sub that read information only from the screen, many parts of the program use this sub.
It is now necessary to call an external application to process this screen information.
That's what generated these cascading changes.
I understand how resumablesub works and its benefits.
But if there was a way (which I've seen that there isn't) to use wait for without the calling routine turning into a resumablesub too, it would save me a lot of work.
Thank you all.
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
you could try something like

B4X:
private sub resumable_shell(obj as object, method as string)
  shell...
  shell...
  result = shell...

  callsubdelayed(obj,method,result)
end sub

private sub a
   shell(me, "a2")
end sub

private sub a2(result)

end sub

private sub b
 shell(me, "b2")
end sub

private sub b2(result)

end sub

private sub c
 shell(me, "c2")
end sub

private sub c2(result)

end sub
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User

I did not understand...
Could you "translate" using the code from post #1?
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
B4X:
Sub OldSub
   
    ' now this sub became resumablesub, because it's calling an old sub that became resumablesub
    ' now, all calls to this sub will become resumablesub, and so on...
   
    NewSub(me,"OldSub2")
End Sub

sub OldSub2(expected_value)
    log(expected_value)
end sub

Sub NewSub(obj as object, method as string)   ' it was just a regular sub before the shell
   
    ' this sub was not resumablesub, now it's needed.
    Dim sh As Shell
    sh.Initialize("sh","WHAREVER.EXE",Null)
    sh.Run(-1)
    Wait For sh_ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)

    callsubdelayed(me, method,stdOut)
End Sub
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
That's it!
I love this community!??
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…