Lazarus

Programming => Operating Systems => Linux => Topic started by: Barry55 on January 23, 2021, 06:35:38 pm

Title: [SOLVED} Issue with TProcess and top
Post by: Barry55 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
Title: [SOLVED] Re: Issue with TProcess and top
Post by: Barry55 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
Title: Re: [SOLVED} Issue with TProcess and top
Post by: PascalDragon on January 24, 2021, 12:05:07 pm
In the future please use code tags (https://wiki.freepascal.org/Forum#Use_code_tags). This results in better readability here on the forum and avoids the forum software potentially messing up code.
TinyPortal © 2005-2018