Recent

Author Topic: Loadfromstream return some lines of text but not all.  (Read 393 times)

Clopidogrel

  • Newbie
  • Posts: 1
Loadfromstream return some lines of text but not all.
« on: July 14, 2025, 01:54:15 pm »
Hi,

I did this code to rsync two folder on demand. The --stat option of rsync should return 16 lines of text and add it to memo1. For some reason, sometimes it return 4 or 8 or 11 lines of text, sometimes it's complete.

monProcess := TProcess.Create(nil);

        monProcess.Executable := ('/usr/bin/rsync');
        if monProcess.Executable <> '' then
          begin
            monProcess.Parameters.Add('-r');
            monProcess.Parameters.Add('--stats');
            monProcess.Parameters.Add('/mnt/8e8f952e-9aaf-4446-9c55-eb7a4e021e7a/Kodisheild/Film/');
            monProcess.Parameters.Add('/home/lmoreau/Mnt/Kodisheild/Film/');
            monProcess.Options := monProcess.Options + [poUsePipes];
            monProcess.Execute;
            Memo1.Lines.add('-------------------------------------------------------------');
            Memo1.Lines.add(DateTimeToStr(Now));

            sl:=TStringList.create;

            sl.LoadFromStream(monProcess.Output);

            for n := 0 to sl.Count - 1 do
                Memo1.Lines.Add(sl[n]);

            sl.free ;
            monProcess.free;
            end;

Thanks to all
                           

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12950
  • FPC developer.
Re: Loadfromstream return some lines of text but not all.
« Reply #1 on: July 14, 2025, 02:02:48 pm »
Yes, this code is wrong. It will only read the maximum number of bytes the OS allows the child program to write to the pipe before blocking.

Easiest is to use Runcommand instead to get the output in a string, and then assign the string to memo.lines.text

In my opinion the "simple example" should be removed from the wiki, as it is simple but incorrect. Read on and see https://wiki.freepascal.org/Executing_External_Programs#Reading_large_output
« Last Edit: July 27, 2025, 06:57:49 pm by marcov »

 

TinyPortal © 2005-2018