[Gambas-user] Blocking read from a Process you had to write to

Benoît Minisini g4mba5 at gmail.com
Tue Mar 19 00:18:22 CET 2019


Le 18/03/2019 à 23:04, Tobias Boege a écrit :
> Hi,
> 
> the task is to start a process, write something to its input, then read
> all its output. This should happen in a blocking fashion, i.e. I want the
> output of the process to be the return value of my function. The process,
> after receiving its input will immediately produce its output. Hence I
> do *not* want to use a Read event, purely because it shouldn't be necessary
> and requires a sort of busy-waiting in my function until the output has
> been accumulated.
> 
> What I normally try first is:
> 
>    Dim sInput As String = "hello" ' might be binary data!
>    Dim aCmd As String[] = ["cat", "-"]
>    Dim hProc As Process, sOut As String
> 
>    hProc = Exec aCmd For Read Write
>    Write #hProc, sInput
>    hProc.CloseInput()
>    hProc.Wait()
>    sOut = Read #hProc, Lof(hProc)
>    Return sOut
> 
> When the Wait call returns, I've got a Lof() of zero, but apparently
> hProc.Value is also zero, i.e. success, so where's my output?

Lost. The Process object won't maintain a read buffer for you, unless 
you use the syntax EXEC ... TO ...

This is why you have to use the Read event and a global buffer.

But I think you can easily embed that management in a class.

Regards,

-- 
Benoît Minisini


More information about the User mailing list