Get the Percentage of Total CPU Usage

后端 未结 5 921
萌比男神i
萌比男神i 2021-01-03 05:00

I am trying to get the % of total CPU usage to a label1.Caption

I\'ve searched and found these:

  • didn\'t work - http://www.vbforums.com

5条回答
  •  醉酒成梦
    2021-01-03 05:12

    I solve this way:

    function TCPU.get_param_value(param_name: String): String;
    var
      command,
      file_out: String;
      data_file: TStringList;
    
    begin
      data_file := TStringList.Create;
      try
        try
          file_out := TPath.GetTempPath + FormatDateTime('yyyymmddhhnnss', Now) + '_CPUInfo.txt';
          comando := '"wmic cpu get '+param_name+' /value | find "'+param_name+'" > ' +
                      file_out + '&&exit"';
    
          // "runas" for admin privileges, or "open" to any user
          ShellExecute(0, 'open', 'cmd.exe', PChar('/k ' + command), nil, SW_HIDE);
    
          // Wait 4 sec to cmd release the process...
          Sleep(4000);
    
          data_file.LoadFromFile(file_out);
          Result := data_file.Values[param_name];
    
        except
          Result := '';
        end;
    
      finally
        TFile.Delete(file_out);
        data_file.Free;
      end;
    

    In this way, you can get any param values from wmic

提交回复
热议问题