Recent

Author Topic: Process Output Read  (Read 1715 times)

BIT

  • Full Member
  • ***
  • Posts: 126
Process Output Read
« on: October 17, 2021, 04:49:57 pm »
Hello, I found the code on the Internet and I have a problem with the output of data from the console.
On the screenshot, the first component of the TreeView1 output is not correct, the second Memo1 output is correct.
I have such a question, why are all components except Memo1 displaying incorrectly?

Code: Pascal  [Select][+][-]
  1. const
  2.   BUF_SIZE = 4096;
  3. // Размер буфера для чтения выходных данных, можно читать блоками по 64k
  4.  
  5. var
  6.   Form1: TForm1;
  7.   AProcess: TProcess;
  8.   ru: boolean;
  9.  
  10. implementation
  11.  
  12. {$R *.lfm}
  13.  
  14. { TForm1 }
  15.  
  16. procedure TForm1.ToggleBox1Change(Sender: TObject);
  17. var
  18.   Buffer: string = '';
  19.   BytesRead: longint = 0;
  20. begin
  21.   //Запуск процесса (выполнение команды dir/ls)
  22.   ru := False;
  23.   Button1.Enabled := True;
  24.   ToggleBox1.Enabled := False;
  25.   AProcess := TProcess.Create(nil);
  26.   AProcess.ShowWindow := swohide;
  27.   AProcess.Executable := 'cmd';
  28.   AProcess.Parameters.Clear;
  29.   AProcess.Parameters.Add('/c');
  30.   AProcess.Parameters.Add(
  31.     'cd compil\System\ & del Core.u del Engine.u & del Nwindow.u & del Interface.u  &echo N|ucc make -NoBind');
  32.   AProcess.Options := [poUsePipes];
  33.   AProcess.Execute;
  34.  
  35.   //можно дать чуть-чуть времени на отправку данных через AProcess.Output
  36.   sleep(500);
  37.   repeat
  38.     Memo1.Lines.BeginUpdate;
  39.     TreeView1.Items.BeginUpdate;
  40.     try
  41.       SetLength(Buffer, BUF_SIZE);
  42.       BytesRead := AProcess.Output.Read(Buffer[1], Length(Buffer));
  43.       if BytesRead > 0 then
  44.       begin
  45.         SetLength(Buffer, BytesRead);
  46.  
  47.         Memo1.Append(Trim(Buffer));
  48.            TreeView1.Items.AddChild(nil,Trim(Buffer));
  49.  
  50.       end;
  51.     finally
  52.       Memo1.Lines.EndUpdate;
  53.       TreeView1.Items.EndUpdate;
  54.       Memo1.SelStart := UTF8Length(Memo1.Text);
  55.     end;
  56.    // Sleep(50);
  57.     Application.ProcessMessages;
  58.     if BytesRead = 0 then
  59.     begin
  60.       ru := True;
  61.       BytesRead := 0;
  62.  
  63.       Buffer := '';
  64.     end;
  65.   until ru = True;
  66. end;                                

jamie

  • Hero Member
  • *****
  • Posts: 5047
Re: Process Output Read
« Reply #1 on: October 17, 2021, 05:16:33 pm »
I would say that is because you are importing <Cr><LF> in the middle of the stream as a single entry as it comes in.

I believe the best way to do this would be either parse the incoming first by looking for the Lind Ending group and if found then read that much up to that point as a line and then the delete that part from the incoming and repeat the process of looking for the Line Ending and if found do it again, if not found then stash that part to a local string where it can be combined later with more incoming..

using a string list maybe a better way to do this.
The only true wisdom is knowing you know nothing

BIT

  • Full Member
  • ***
  • Posts: 126
Re: Process Output Read
« Reply #2 on: October 17, 2021, 06:28:22 pm »
I would say that is because you are importing <Cr><LF> in the middle of the stream as a single entry as it comes in.

I believe the best way to do this would be either parse the incoming first by looking for the Lind Ending group and if found then read that much up to that point as a line and then the delete that part from the incoming and repeat the process of looking for the Line Ending and if found do it again, if not found then stash that part to a local string where it can be combined later with more incoming..

using a string list maybe a better way to do this.
OK! I will add thanks!

 

TinyPortal © 2005-2018