Page 1 of 1

RunProgram problem

Posted: Mon Feb 04, 2013 6:08 pm
by Yewklid
I would like to use RunProgram to run a process and collect the output. The problem is that PureBasic seems to hold the output in a buffer and only releases it when the process ends.
These two PureBasic programmes show what I want:

Code: Select all

;Prog1
If OpenConsole()
  
  For i = 1 To 10
    A$ = Input()
    A$ = "> " + A$
    PrintN(A$)
  Next
  
EndIf 
Prog1 is compiled and placed on in the same folder as Progtest.

Code: Select all

;Progtest
Quit = #False
program$ = "Prog1.exe"
cmd$ = ""

Prog = RunProgram(program$, cmd$, "" ,#PB_Program_Open |#PB_Program_Read|#PB_Program_Write|#PB_Program_Hide)

If Prog
  String$ = "print 37" 
EndIf 

If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(0, 10, 10, 200, 20, "Press")
 EndIf
  
Repeat 
    
  Event = WaitWindowEvent(30)
  If Event = #PB_Event_CloseWindow
    quit = #True
  EndIf
  If Event = #PB_Event_Gadget     
    Select EventGadget()
      Case 0
        R = WriteProgramStringN(Prog, String$)                
    EndSelect
  EndIf   
  O$ = ""    
  If ProgramRunning(Prog) And AvailableProgramOutput(Prog)   
    O$ = O$  +  ReadProgramString(Prog)
    Debug O$   
  EndIf
  
Until Quit = #True  
  
KillProgram(Prog)
CloseProgram(Prog) 
Debug "closed"
  
These work correctly. When I click on the button, the output appears immediately in the debug window.
When I replace prog1 by a programme written in a different language which does exactly the same thing, no output apears in the debug window until the programme ends and then all 10 outputs appear at once.

At first I thought the other language programme must be at fault, but when I run it from the command prompt it behaves correctly. When I type something the output appears immediately.

Can anyone tell me if PureBasic does hold the output in a buffer and, if so, is there anything I can do about it?

Mike

Re: RunProgram problem

Posted: Mon Feb 04, 2013 7:49 pm
by IdeasVacuum
Running in debug might actually contribute to the issue, so compile your app, see if that makes a difference (you can have the other app's data displayed with a Message Requester for now).

Re: RunProgram problem

Posted: Mon Feb 04, 2013 7:59 pm
by freak
ReadProgramString() blocks until a full line of input is available (i.e. a linebreak is sent). If you need to receive input before the linebreak is sent, you can try with ReadProgramData() which does not block at all.

However, generally in a case like this it is the sending program which does the buffering, and it buffers more than one line. Most frameworks do buffering unless you explicitly force a write using a flush command. The PB example works because PrintN() explicitly flushes the output pipe after each line.

Re: RunProgram problem

Posted: Mon Feb 04, 2013 10:21 pm
by Yewklid
Thank you both for your suggestions. Unfortunately no luck with either. (I tried them seperately and together)

The puzzling thing (in view of freak's comments) is that when I run the other program on its own from the command line it returns each input immediately.

Re: RunProgram problem

Posted: Mon Feb 04, 2013 10:27 pm
by freak
A program can detect if it is run interactively (from a commandline) and then act differently. Dunno if this is the case here though.

Re: RunProgram problem

Posted: Mon Feb 04, 2013 10:32 pm
by Yewklid
I'll try to find out. Thanks.