B4J Question jShell Wait for not firing

MathiasM

Active Member
Licensed User
Hello

I try to get Shell output, but the Wait For never seems to fire, altough a TimeOut is set. I have this in my main code:
B4X:
Sub AppStart (Args() As String)
    Start
    StartMessageLoop
End Sub

Sub Start()
    Wait For (Google.GetUsers("xxxxxxxxx.com")) Complete ()
    Log("Done")
End Sub

I have a module named Google with a GetUsers resumable sub:

B4X:
Public Sub GetUsers(DomainName As String) As ResumableSub
    Log("Starting GetUsers")
    Dim sh As Shell
    sh.Initialize("", "GAM", Array As String("print", "users", "domain", DomainName))
    sh.RunWithOutputEvents(10000)
    Wait For (sh) ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("Worked")
        Log(StdOut)
    Else
        Log("Error")
        Log(StdErr)
    End If
    Log("GetUsers finished")
    Return True
End Sub

I'm pretty sure the initialize is correct (I have used the GAM tool many times, this is the first time I wrote a method that actually needs to fetch a result)
Even if something is terrible wrong, I would expect the RunWithOutputEvents to timeout after 10 seconds and show something, but it doesn't.

The only log I get is Starting GetUsers. After that the code just keeps on running forever.

Is their something wrong with my Resumable Sub?
Thanks a lot.
 
Solution
1. Better to use Run instead of RunWithOutputEvents (I don't see that you are handling the StdOut / StdErr events).
2. You should always add a parameter when waiting for resumable subs:
B4X:
  Wait For (Google.GetUsers("xxxxxxxxx.com")) Complete (Unused As Boolean)
3. You need to set the event name and catch the event correctly:
B4X:
   sh.Initialize("sh",...
   sh.Run(10000)
    Wait For (sh) sh_ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Better to use Run instead of RunWithOutputEvents (I don't see that you are handling the StdOut / StdErr events).
2. You should always add a parameter when waiting for resumable subs:
B4X:
  Wait For (Google.GetUsers("xxxxxxxxx.com")) Complete (Unused As Boolean)
3. You need to set the event name and catch the event correctly:
B4X:
   sh.Initialize("sh",...
   sh.Run(10000)
    Wait For (sh) sh_ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
 
Upvote 1
Solution

MathiasM

Active Member
Licensed User
Thanks @Erel for your reply, it does work now indeed.
I tend to not use (Unused As boolean) because I have plenty of them, and using Unused1, Unused2, Unused3 seems so redundant, I never had problems with it, are there edge cases where it can result in wrong behaviour?

I based my Wait For code on this topic: Resumable Subs

There you specified
Wait For (<sender>) <event signature>
With example:
B4X:
Wait For (job) JobDone(job As HttpJob)

Why do I have to specify sh_ProcessCompleted() and you didn't need job_JobDone()?

When I read my own comments I sound rather rude. But I'm not, I really try to understand and my English is not that good. I hope you understand.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I tend to not use (Unused As boolean) because I have plenty of them, and using Unused1, Unused2, Unused3 seems so redundant, I never had problems with it, are there edge cases where it can result in wrong behaviour?
You can use the same "unused" variable all the time.

I never had problems with it, are there edge cases where it can result in wrong behaviour?
This is part of the expected syntax. It will definitely not work in B4i and there might be other cases where it fail.

Why do I have to specify sh_ProcessCompleted() and you didn't need job_JobDone()?
I'll explain:
Wait For can be used to catch events and resumable subs.
By convention events signature is made of <event name prefix>_<event>.
JobDone signature comes from the very old HttpUtils 1 module.
Note that this has nothing to do with Wait For.
B4X:
Sub JobDone (j As HttpJob)
'compared to:
Sub sh_ProcessCompleted(...)
 
Upvote 0

Similar Threads

Top