RunProgram problem

Just starting out? Need help? Post your questions and find answers here.
Yewklid
User
User
Posts: 19
Joined: Sun May 27, 2007 3:40 pm
Location: Berkshire, England

RunProgram problem

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: RunProgram problem

Post 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).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: RunProgram problem

Post 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.
quidquid Latine dictum sit altum videtur
Yewklid
User
User
Posts: 19
Joined: Sun May 27, 2007 3:40 pm
Location: Berkshire, England

Re: RunProgram problem

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: RunProgram problem

Post 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.
quidquid Latine dictum sit altum videtur
Yewklid
User
User
Posts: 19
Joined: Sun May 27, 2007 3:40 pm
Location: Berkshire, England

Re: RunProgram problem

Post by Yewklid »

I'll try to find out. Thanks.
Post Reply