Missed output from progrum run by RunProgram?

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 2073
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Missed output from progrum run by RunProgram?

Post 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).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
User avatar
ChrisR
Addict
Addict
Posts: 1576
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Missed output from progrum run by RunProgram?

Post 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.
infratec
Always Here
Always Here
Posts: 7781
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Missed output from progrum run by RunProgram?

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2073
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Missed output from progrum run by RunProgram?

Post 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!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Axolotl
Addict
Addict
Posts: 924
Joined: Wed Dec 31, 2008 3:36 pm

Re: Missed output from progrum run by RunProgram?

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
infratec
Always Here
Always Here
Posts: 7781
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Missed output from progrum run by RunProgram?

Post 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$)
Post Reply