Forum > Linux
TProcess does not work in Linux
svd71:
Hi All,
I had make little example of the program, that does not work.
How somebody advice me show what I did wrong?
--- 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";}};} ---program testProcess; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, SysUtils, CustApp, process, FileUtil { you can add units after this }; type { TTest } TTest = class(TCustomApplication) protected procedure DoRun; override; public ScriptProcess : TProcess; procedure Execute; constructor Create(TheOwner: TComponent); override; destructor Destroy; override; end; { TTest } procedure TTest.Execute;var ras,cmd : String; sl : TStringList; i : Integer;begin sl := TStringList.Create; try ScriptProcess := TProcess.Create(nil);// ScriptProcess.ShowWindow:= swoNone; {$ifdef unix} cmd := FindDefaultExecutablePath('ls'); {$endif} ras := '--help'; ScriptProcess.Executable:= cmd; ScriptProcess.Parameters.add('-c'); ScriptProcess.Parameters.add(ras); ScriptProcess.Options := [poNoConsole, poUsePipes ]; try ScriptProcess.Execute; except on E: Exception do Writeln('SE:E:' + E.Message); end; while ScriptProcess.Running do begin if ScriptProcess.Output.NumBytesAvailable > 0 then begin sl.LoadFromStream(ScriptProcess.Output); Writeln(sl.Text); end; if ScriptProcess.Stderr.NumBytesAvailable > 0 then begin sl.LoadFromStream(ScriptProcess.Stderr); Writeln(sl.Text); end; Sleep(100); end; finally ScriptProcess.Free; sl.free; end; Writeln('');end; procedure TTest.DoRun;var ErrorMsg: String;begin // quick check parameters ErrorMsg:=CheckOptions('h', 'help'); if ErrorMsg<>'' then begin ShowException(Exception.Create(ErrorMsg)); Terminate; Exit; end; Execute; // stop program loop Terminate;end; constructor TTest.Create(TheOwner: TComponent);begin inherited Create(TheOwner);end; destructor TTest.Destroy;begin inherited Destroy;end; var Application: TTest;begin Application:=TTest.Create(nil); Application.Title:='My Application'; Application.Run; Application.Free;end.
Fred vS:
Hello.
With this it works here (XUbuntu 64 bit):
--- 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";}};} --- procedure TTest.Execute; var ras,cmd : String; sl : TStringList; i : Integer;begin sl := TStringList.Create; try ScriptProcess := TProcess.Create(nil); {$ifdef unix} cmd := FindDefaultExecutablePath('ls'); {$endif} ras := '--help'; ScriptProcess.Executable:= cmd; ScriptProcess.Parameters.add('-c'); ScriptProcess.Parameters.add(ras); ScriptProcess.Options := [poNoConsole, poUsePipes]; ScriptProcess.Execute; sl.LoadFromStream(ScriptProcess.Output); Writeln(sl.Text); finally ScriptProcess.Free; sl.free; end; Writeln(''); end;
marcov:
sl.LoadFromStream(ScriptProcess.Output) doesn't get the number of bytes saved, so it is probably not safe. Also calling it potentially multiple times doesn't seem something that TStringlist.loadfromstream supports. So badly designed code.
The base principle is to read _all_ bytes indicated by numbytesavailable(*), and never read more.
Fred vS:
--- Quote from: marcov on April 24, 2023, 09:01:07 pm ---sl.LoadFromStream(ScriptProcess.Output) doesn't get the number of bytes saved, so it is probably not safe. Also calling it potentially multiple times doesn't seem something that TStringlist.loadfromstream supports. So badly designed code.
The base principle is to read _all_ bytes indicated by numbytesavailable(*), and never read more.
--- End quote ---
I try to catch the goal of ScriptProcess.Output.NumBytesAvailable
With this:
--- 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";}};} --- ... ScriptProcess.Options := [poNoConsole, poUsePipes]; ScriptProcess.Execute; writeln(scriptProcess.Output.NumBytesAvailable); sl.LoadFromStream(ScriptProcess.Output); writeln(scriptProcess.Output.NumBytesAvailable);
I get this:
--- Code: Bash [+][-]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";}};} ---00
But with this ( adding poWaitOnExit ):
--- 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";}};} --- ... ScriptProcess.Options := [poNoConsole, poWaitOnExit, poUsePipes]; ScriptProcess.Execute; writeln(scriptProcess.Output.NumBytesAvailable); sl.LoadFromStream(ScriptProcess.Output); writeln(scriptProcess.Output.NumBytesAvailable);
I get this:
--- Code: Bash [+][-]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";}};} ---77530
What are those 7753 bytes available for output after ScriptProcess.Execute and why 0 after sl.LoadfromStream ?
marcov:
As said, sl.loadfromstream tries to read with 1024 bytes increments, and thus will read beyond numbytes. Therefore this is nonsense code.
Navigation
[0] Message Index
[#] Next page