Reading TProcess.Output lazarus / 8-Other ---------------------------------------------------------------------- PaulAtreides62 · 2009-04-05 20:38 Hi al all. I have problems with the reading from Tprocess.Output ( TinputPipeStream type) effected in my Lazarus application. Particularly the reading of TProcess.Output happens inside a Thread where an external console program is performed, "prova1.exe", through the use of a TProcess object. The external program writes in the standard channel of output some text. I bring part of the code of my application Code: [Select]  ...   type   TTestThread = class(TThread)   private     Processo1:Tprocess;     s:string;   protected    ...   procedure Execute; override;   end; implementation   ...  procedure TTestThread.Execute; var    n: LongInt;    ProntinLettura, i:Longint;    buf:char;    stringaoutput:string; begin    FreeOnTerminate := True;    ...    Processo1:=Tprocess.Create(nil) ;    Processo1.CommandLine :='prova1.exe';    Processo1.Options := [poUsePipes];    Processo1.ShowWindow := swoHIDE;    Processo1.Priority :=ppNormal;    stringaoutput:='';    try      Processo1.Execute;    except      ....    end;    while Processo1.Running do    begin    Prontinlettura:=0;    repeat      if Fine then Terminate;      if Terminated then            begin            ...            end;      Prontinlettura := Processo1.Output.NumBytesAvailable  ;    until ProntinLettura > 0; ****************************************************************    for i:=1 to ProntinLettura do       begin       n := Processo1.Output.Read(Buf, 1);       if (buf<>#10) and (buf<>#13) then          stringaoutput:=stringaoutput+buf       else          begin          s:=stringaoutput;          stringaoutput:='';          if s<>'' then             synchronize(@Appendi);          end;       end; ****************************************************************    end; // qui il programma esterno è terminato...    ... The underlined code should allow the reading of the text produced by the program prova1.exe also when this is in execution, instead, in reality, the capture of the output happens later only, when prova1.exe has finished its execution. How come? Is there a way to make up for? Thanks to everybody ---------------------------------------------------------------------- Leledumbo · 2009-04-06 05:08 Not really sure, perhaps because you call Execute around try-except. ---------------------------------------------------------------------- PaulAtreides62 · 2009-04-06 07:17 Hi Leledumbo; I have removed the block try-except, but the problem has remained. Does someone have other ideas? ---------------------------------------------------------------------- Vincent Snijders · 2009-04-06 07:22 Does the called process flush its output? Or will it remain in its buffer until it closes? ---------------------------------------------------------------------- PaulAtreides62 · 2009-04-06 13:28 Hi Vincent. The process, if I have well understands what you ask me, flushs his output while it's still running, and not only at the end of the "running". ---------------------------------------------------------------------- Vincent Snijders · 2009-04-06 13:34 It depends on the amount of data the proces writes. If it is less small (and a fpc program), it remains in the buffer of rtl (128 bytes, IIRC), the OS may buffer 2,4 or even 64 kB, I don't know the details. ---------------------------------------------------------------------- PaulAtreides62 · 2009-04-06 20:21 The process writes no more then 10 Kbytes, i think, and the time of execution depends on numeric data read by a text file specified by a command line string. Some times almost instantly finishes, other times the execution is much longer but, while running, the program keeps on writing text on standard output channel ( I verified launching it in a dos shell ). I know that an application, similar to mine but written with visual c++, doesn't have this problem executing this external program. The external program is compiled by borland c++ ( last version). The buffer depends on the compiler, the OS, or both? ---------------------------------------------------------------------- Vincent Snijders · 2009-04-06 20:25 Both. ---------------------------------------------------------------------- PaulAtreides62 · 2009-04-07 07:38 I can change the compiler of the external program, or is there something of anything else other than can I do? ---------------------------------------------------------------------- Vincent Snijders · 2009-04-07 10:49 If it is a freepascal program, you can call Flush. ---------------------------------------------------------------------- PaulAtreides62 · 2009-04-08 07:18 Hi, Vincent. I will make some other test, eventually modifying the code of the external program, and i will see what it happens. thanks