B4J Question Wait for a .exe to finish executing

Cal4th

Member
I am executing a .exe with b4j and this .exe is writing me a file while it is executing then it stops and the file finishes writing.i an trying to read this file but since it does not finishes when my line of reading executes then it only reads part of it. Is there a way to wait for the .exe to finish so i can read the hole file written by it?
 

Cal4th

Member
Hello Erel,

I am using a .bat to run the .exe since because if i run the .exe directly it takes more space. the code you rote could work with a .bat too? cause i am trying to use it but it does not wait...
it just jumps and never returns to the sub where i have your code.
I have tried using it in different ways:

Way 1:
B4X:
run(".bat",Null)

Sub run (EXE As String, code As List)    As ResumableSub
    Dim shl As Shell
    shl.Initialize("shl",EXE,code)
    shl.Run(-1)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(Success)
    Log(StdOut)
    Log(StdErr)

'
    Return ""
    End Sub

Way 2:
B4X:
    Dim shl As Shell
    shl.Initialize("shl",".bat",Null)
    shl.Run(-1)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(Success)
    Log(StdOut)
    Log(StdErr)

all this is inside a sub of a function that is inside a loop since I'm iterating many times through the launch if the .bat. So, what "Wait for" does is to go out of the sub where it is and then doo the iterative process. this causes the wait for to never be completed since it will complete until finishing the sub that contains the loop.

I hope i explained myself right so that you can understand and help.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
See the video tutorial about resumable subs: https://www.b4x.com/etp.html
It is a very important concept.

It works inside loops.
It is better to set the sender filter:
B4X:
 Wait For (shl) shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)

If the event is never raised then it means that the process never ended.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
f the program is executed with a .bat, it only uses the necessary to solve what i'm asking him
How? Can you provide an example?
 
Upvote 0
Top