Recent

Author Topic: [SOLVED} Issue with TProcess and top  (Read 1226 times)

Barry55

  • New Member
  • *
  • Posts: 20
[SOLVED} Issue with TProcess and top
« on: January 23, 2021, 06:35:38 pm »
I am trying to use TProcess to get some information about what is happening on my Raspberry pi. Some of it is working fine. the following code is OK

Var
  AProcess        : TProcess;
  AStringList     : TStringList;
  ResultStr        : String;

Begin
  AStringList := TStringList.Create;
  AProcess := TProcess.Create(nil);
  ResultStr := '';

  Try
    AProcess.Executable := 'vcgencmd';
    Aprocess.Parameters.Add('measure_temp');
    AProcess.Options := [poWaitOnExit, poUsePipes];
    AProcess.Execute;

    AStringList.LoadFromStream(AProcess.Output);
    ResultStr := AStringList.Text;
  finally
    AStringList.Free;
    AProcess.Free;
  end;

and I get the information I expected in ResultStr. However if I try the following

  AStringList := TStringList.Create;
   AProcess := TProcess.Create(nil);
   ResultStr := '';

   Try
     AProcess.Executable := '/usr/bin/top';
     Aprocess.Parameters.Add('-n 1 -p0');
     AProcess.Options := [poWaitOnExit, poUsePipes];
     AProcess.Execute;

     AStringList.LoadFromStream(AProcess.Output);
     ResultStr := AStringList.Text;
   finally
     AStringList.Free;
     AProcess.Free;
   end;

ResultStr is always empty. I assume it might be something to do with top clearing the screen before it starts but I am not sure how to get round this and wondered if anyone had any ideas.

Many thanks

Regards

Barry
« Last Edit: January 23, 2021, 07:09:49 pm by Barry55 »

Barry55

  • New Member
  • *
  • Posts: 20
[SOLVED] Re: Issue with TProcess and top
« Reply #1 on: January 23, 2021, 07:09:32 pm »
Sorry - went back to the documentation and found the issue, I needed to do this to make it work

   AStringList := TStringList.Create;
   AProcess := TProcess.Create(nil);
   ResultStr := '';

   Try
     AProcess.Executable := '/usr/bin/top';
     Aprocess.Parameters.Add('-n1');
     Aprocess.Parameters.Add('-p0');;
     Aprocess.Parameters.Add('-b');
     AProcess.Options := [poWaitOnExit, poUsePipes];
     AProcess.Execute;

     AStringList.LoadFromStream(AProcess.Output);
     ResultStr := AStringList.Text;
   finally
     AStringList.Free;
     AProcess.Free;
   end;

Barry

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [SOLVED} Issue with TProcess and top
« Reply #2 on: January 24, 2021, 12:05:07 pm »
In the future please use code tags. This results in better readability here on the forum and avoids the forum software potentially messing up code.

 

TinyPortal © 2005-2018