B4J Question How to use wait for when result expectations can be multiple

kostefar

Active Member
Licensed User
Longtime User
Dear All,

Sorry if that´s a confusing header.. I´ll try to explain:

I have this:

B4X:
                Dim shl As Shell
                        shl.Initialize("shl", "py", Array As String("C:\python\getchannelid.py",channame))                           
                        shl.InputStreamEnabled = True
                        shl.RunWithOutputEvents(-1)
                        
                        Wait For (shl) shl_StdOut (Buffer() As Byte, Length As Int)
                        Dim Output As String = BytesToString(Buffer,0,Length,"UTF-8")
                        Log (Output)

This works great when nothing is wrong, but sometimes an error is thrown, and that´s intentional. In that case, the output ends up in shl_StdErr.
Is there a way to wait for an incoming message in either one of stdOut OR StdErr?
 

kostefar

Active Member
Licensed User
Longtime User
Are you sure that you want to handle both StdOut and StdErr events? In most cases it will be simpler to call shl.Run and wait for the ProcessCompleted event.

StdOut and StdErr can be raised multiple times and they are raised on a different thread. They shouldn't be used in most cases.

Erel, that´s such a much smarter way of doing this - thanks for making me aware! Just tested it on both a valid and an invalid input and it does just what I want it to!
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
I'm trying to wait for the Process Completed event using the code below, but the "wait for" never gets run.

This code worked OK when the ProcessedCompleted event was a separate sub.

B4X:
    Log("Getting number")

    Dim WkgDir As String = "C:\temp"
    Dim out As OutputStream = File.OpenOutput(WkgDir, "test.png", False)
    IM.WriteToStream(out)
    out.Close
    Dim shl As Shell
    Dim td As String ="C:\Tesseract"
    shl.Initialize("shl", "Tesseract", _
     Array As String(WkgDir & "\test.png", WkgDir & "\out", "-psm", "7"))
    shl.WorkingDirectory = td
    shl.Run(5000) 'set a timeout of 5 seconds

    
wait For (shl) sh1_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    'read the output
    Log("Process completed")

Any suggestions please on how to fix this? I'd prefer to use the Wait For method.

Derek
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Problem solved.:) It was some sort of compiler glitch. I noticed that the compiler was a bit confused about whether sh1 was defined or not. I renamed sh1 to shx and everything started working. The symptoms were that autocomplete did not work when I typed sh1, but the dropdown for the characters "sh" offered sh1 as an option.

Great thing Wait For! :)
 
Upvote 0
Top