Page 1 of 1

Disabling buffering at RunProgram

Posted: Mon Jan 18, 2021 7:41 pm
by wb2k
Does anyone know how I can disable buffering at RunProgram?

When I execute gpupdate in an command-window it writes "Updating policy ..." immediately after I started it, but when executed via PureBasic's RunProgram it looks like there is no output until the gpupdate exits:

Code: Select all

handle = RunProgram("gpupdate", "", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Error)
While ProgramRunning(handle)
  Debug "Running ..."
  If AvailableProgramOutput(handle)
    Debug "Output: " + ReadProgramString(handle)
  EndIf
  Delay(500)
Wend

Re: Disabling buffering at RunProgram

Posted: Mon Jan 18, 2021 8:07 pm
by Kiffi
Some programs write their output to stderr. Try ReadProgramError().

Re: Disabling buffering at RunProgram

Posted: Mon Jan 18, 2021 8:18 pm
by wb2k
Kiffi wrote:Some programs write their output to stderr. Try ReadProgramError().
stderr is empty, gpupdate writes to stdout.
The expected output is there at ReadProgramString(), but too late.

Re: Disabling buffering at RunProgram

Posted: Mon Jan 18, 2021 9:59 pm
by Ajm
If you try with ReadProgramData does that receive it. Maybe gpupdate is not terminating the string until the end.

Re: Disabling buffering at RunProgram

Posted: Mon Jan 18, 2021 10:41 pm
by wb2k
Ajm wrote:If you try with ReadProgramData does that receive it. Maybe gpupdate is not terminating the string until the end.
AvailableProgramOutput() would return true if there is any output (even non-terminating).
In the test-code it is executing the If-branch only near the end of gpupdate, so ReadProgramData won't make any difference.

Re: Disabling buffering at RunProgram

Posted: Tue Jan 19, 2021 3:46 pm
by Axolotl
wb2k, if I were you I would give the suggestion made by Ajm a try.

IMHO a closer look at the documentation gives some advice about that.

https://www.purebasic.com/documentation ... utput.html

Result = AvailableProgramOutput(Program)
The number of bytes available to be read from the programs output.

https://www.purebasic.com/documentation ... tring.html

Result$ = ReadProgramString(Program [, Flags])
This function also waits until a full line of output is available. If not line-based or raw output is to be read, ReadProgramData() may be used.

This could be interpreted as waiting for a cr+lf, don't you think?

Re: Disabling buffering at RunProgram

Posted: Tue Jan 19, 2021 4:03 pm
by BarryG
It's just a cmd box issue, because if you redirect the output like this from a cmd box:

Code: Select all

gpupdate >c:\out.txt
Then the text file is NOT updated until the very end, either - just like PureBasic.

Same if you send the output to the clipboard with "gpupdate | clip" - the clipboard doesn't get updated until the very end, again just like PureBasic.

Conclusion: Nothing you can do, wb2k.