Page 1 of 1

Missed output from progrum run by RunProgram?

Posted: Fri Jan 30, 2026 7:01 pm
by jacdelad
This example is from the help:

Code: Select all

  ; Executes the PB compiler with the /? option and displays the output
  ;
  Compiler = RunProgram(#PB_Compiler_Home+"compilers/pbcompiler", "-h", "", #PB_Program_Open | #PB_Program_Read)
  Output$ = ""
  If Compiler
    While ProgramRunning(Compiler)
      If AvailableProgramOutput(Compiler)
        Output$ + ReadProgramString(Compiler) + Chr(13)
      EndIf
    Wend
    Output$ + Chr(13) + Chr(13)
    Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
    
    CloseProgram(Compiler) ; Close the connection to the program
  EndIf
  
  MessageRequester("Output", Output$)
https://www.purebasic.com/documentation ... ogram.html

What happens if the program spits out output right before the wend? Wouldn't it be missed? Even more, I usually us a while construct within the ProgrammRunning-loop to get the output (unrelated to my question, but maybe better timing).

Re: Missed output from progrum run by RunProgram?

Posted: Fri Jan 30, 2026 7:25 pm
by ChrisR
With #PB_Program_Open(Read) the program is running for your exe, as long as there is something to read.

From ProgramRunning help:
it will return nonzero as long there is something to read, even if the program is already ended.

Re: Missed output from progrum run by RunProgram?

Posted: Fri Jan 30, 2026 8:23 pm
by infratec
Your handling is not 100% correct.

AvailableProgramOutput() returns the bytes to read.
If you use ReadProgramString() you can still 'lock' the program, because it waits for a CR.
You should use ReadProgramData() and PeekS().
Also you should read stderr, because many programs are writing also some stuff via this way.

viewtopic.php?t=64876

Re: Missed output from progrum run by RunProgram?

Posted: Fri Jan 30, 2026 9:57 pm
by jacdelad
@ChrisR: Thanks, I overread this.
@infratec: Well, that's the example from the help and I didn't have any problems yet. Maybe it should be updated.

Thanks you both!

Re: Missed output from progrum run by RunProgram?

Posted: Sat Jan 31, 2026 4:23 pm
by Axolotl
I have the same situation/problem when calling the pb compiler with RunProgram().
And in this particular case, it is not because CRLF is being waited for during reading, because the same result is obtained with ReadProgramData().
See also: Command Prompt vs. RunProgram Output

My solution (which probably only works for me) is to use the combination of OpenConsole(), _wsystem(), and CloseConsole().
I admit, it's not pretty, but it's rare.

Re: Missed output from progrum run by RunProgram?

Posted: Sat Jan 31, 2026 5:08 pm
by infratec
Btw. your code works on my PC.

Code: Select all

; Executes the PB compiler with the -h option and displays the output

Compiler = RunProgram(#PB_Compiler_Home + "compilers/pbcompiler", "-h", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Error)
Output$ = ""
Error$ = ""
If Compiler
  *Buffer = AllocateMemory(16348, #PB_Memory_NoClear)
  If *Buffer
    While ProgramRunning(Compiler)
      OutputSize = AvailableProgramOutput(Compiler)
      While OutputSize
        ReadSize = OutputSize
        If ReadSize > MemorySize(*Buffer)
          ReadSize = MemorySize(*Buffer)
        EndIf
        If ReadProgramData(Compiler, *Buffer, ReadSize) = ReadSize
          Output$ + PeekS(*Buffer, ReadSize, #PB_UTF8|#PB_ByteLength)
        EndIf
        OutputSize - ReadSize
      Wend
      Error$ + ReadProgramError(Compiler, #PB_UTF8)
    Wend
    FreeMemory(*Buffer)
  EndIf
  Output$ + #LF$ + #LF$
  Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
  
  CloseProgram(Compiler) ; Close the connection to the program
EndIf

;Debug Output$

MessageRequester("Output", Output$)
MessageRequester("Error", Error$)