Recent

Author Topic: TProcess !? little problems  (Read 1564 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 542
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
TProcess !? little problems
« on: August 20, 2017, 03:26:09 pm »
here is my code for using TProcess.

Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, process, FileUtil, Forms, Controls, Graphics,
  10.   Dialogs, StdCtrls;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1 : TButton;
  18.     Memo1 : TMemo;
  19.     pr12 : TProcess;
  20.     pr1 : TProcess;
  21.     procedure Button1Click(Sender : TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     procedure am(const str: string );
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1 : TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. const
  37.   BSIZE = 2048; // Buffer size for reading the output in chunks
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.Button1Click(Sender : TObject);
  42. var
  43.   os : TMemoryStream;
  44.   BRead    : longint;
  45.   Buf       : array[1..BSIZE] of byte;
  46.   ls: TStringList;
  47.   I : Integer;
  48. begin
  49.  
  50.   pr12.Execute;
  51.   BRead := 0;
  52.   os := TMemoryStream.Create;
  53.   ls := TStringList.Create;
  54.   try
  55.     repeat
  56.       Application.ProcessMessages;
  57.       BRead := 0;
  58.       os.clear;
  59.       ls.Clear;
  60.       BRead := pr12.Output.Read(Buf, BSIZE);
  61.       if BRead = 0 then Break;
  62.       os.Write(Buf, BRead);
  63.       // write to list
  64.       os.Position := 0;
  65.       ls.LoadFromStream(os);
  66.       for I:=0 to Pred(ls.Count) do begin
  67.         am(ls.Strings[I]);
  68.         Application.ProcessMessages;
  69.       end;
  70.       Application.ProcessMessages;
  71.       Sleep(100);
  72.     until BRead = 0;
  73.  
  74.   finally
  75.     os.Free;
  76.     ls.Free;
  77.   end;
  78.  
  79. // WORKS ... not so good
  80.   //while pr12.Running do begin
  81.   //  Application.ProcessMessages;
  82.   //  Memo1.Lines.LoadFromStream(pr12.Output);
  83.   //end;
  84. end;
  85.  
  86. procedure TForm1.am(const str : string);
  87. begin
  88.   Memo1.Lines.Add(str);
  89. end;
  90.  
  91. end.
  92.  
  93. { }
  94.  

It works and display the final result of the operation.
But What I want is to show the result of the operation in sychronisation of the running process, something like a prograss on curl command line .

In my example I use upx to compress an EXE file but it shows me the final result and not the pregression of the compression of the file.


So, Any help ??
« Last Edit: August 20, 2017, 03:29:28 pm by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: TProcess !? little problems
« Reply #1 on: August 20, 2017, 03:47:31 pm »
I have to look into it further and it probably does not matter but it is not a very good idea to call your string variable str because of https://www.freepascal.org/docs-html/rtl/system/str.html
That's a system procedure....
Specialize a type, not a var.

BSaidus

  • Hero Member
  • *****
  • Posts: 542
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: TProcess !? little problems
« Reply #2 on: August 20, 2017, 04:44:07 pm »
done !!
replaced
Code: Pascal  [Select][+][-]
  1. str
with
Code: Pascal  [Select][+][-]
  1. psstr
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018