Forum > Beginners

Process Output Read

(1/1)

BIT:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  BUF_SIZE = 4096;// Размер буфера для чтения выходных данных, можно читать блоками по 64k var  Form1: TForm1;  AProcess: TProcess;  ru: boolean; implementation {$R *.lfm} { TForm1 } procedure TForm1.ToggleBox1Change(Sender: TObject);var  Buffer: string = '';  BytesRead: longint = 0;begin  //Запуск процесса (выполнение команды dir/ls)  ru := False;  Button1.Enabled := True;  ToggleBox1.Enabled := False;  AProcess := TProcess.Create(nil);  AProcess.ShowWindow := swohide;  AProcess.Executable := 'cmd';  AProcess.Parameters.Clear;  AProcess.Parameters.Add('/c');  AProcess.Parameters.Add(    'cd compil\System\ & del Core.u del Engine.u & del Nwindow.u & del Interface.u  &echo N|ucc make -NoBind');  AProcess.Options := [poUsePipes];  AProcess.Execute;   //можно дать чуть-чуть времени на отправку данных через AProcess.Output  sleep(500);  repeat    Memo1.Lines.BeginUpdate;    TreeView1.Items.BeginUpdate;    try      SetLength(Buffer, BUF_SIZE);      BytesRead := AProcess.Output.Read(Buffer[1], Length(Buffer));      if BytesRead > 0 then      begin        SetLength(Buffer, BytesRead);         Memo1.Append(Trim(Buffer));           TreeView1.Items.AddChild(nil,Trim(Buffer));       end;    finally      Memo1.Lines.EndUpdate;      TreeView1.Items.EndUpdate;      Memo1.SelStart := UTF8Length(Memo1.Text);    end;   // Sleep(50);    Application.ProcessMessages;    if BytesRead = 0 then    begin      ru := True;      BytesRead := 0;       Buffer := '';    end;  until ru = True;end;                                

jamie:
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.

BIT:

--- Quote from: jamie 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.

--- End quote ---
OK! I will add thanks!

Navigation

[0] Message Index

Go to full version