Delphi Easily Monitor CPU Usage Effectively From Your Delphi Windows Apps With This Powerful Component Suite

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Easily Monitor CPU Usage Effectively From Your Delphi Windows Apps With This Powerful Component Suite
February 8, 2021 By Anbarasan

Monitoring CPU usage becomes essential use case in some of the applications. Don’t know how to monitor the CPU usage in your Delphi Applications? Don’t worry. Для просмотра ссылки Войди или Зарегистрируйся System Information Management Suite’s component helps to Monitor CPU usage easily and we will learn how to use the thread TPerfMonThread in this post.

Platforms: Windows.

Installation Steps:

You can easily install this Component Suite from GetIt Package Manager. The steps are as follows.
  1. Navigate In RAD Studio IDE->Tools->GetIt Package Manager->select Components in Categories->Components->Trail -MiTec system Information Component Suite 14.3 and click Install Button.
  2. Read the license and Click Agree All. An Information dialog saying ‘Requires a restart of RAD studio at the end of the process. Do you want to proceed? click yes and continue.
  3. It will download the plugin and installs it. Once installed Click Restart now.
How to run the Demo app:
  • Navigate to the System Information Management Suite trails setup, Demos folder which is installed during Get It installation e.g) C:UsersDocumentsEmbarcaderoStudio21.0CatalogRepositoryMiTeC-14.3DemosDelphi21
  • Open the CPUUsage project in RAD studio 10.4.1 compile and Run the application.
  • This Demo App shows how to monitor the Performance of CPU in your machine and show the performance by progress bar.
Components used in MSIC CPUUsage Demo App:
  • TPerfMonThread: Monitors specified performance counters.
  • TGuage: To show the performance of the CPU
  • TLabel: To represent the CPU core names.
Implementation Details:
  • An instance FPM of TPerfMonThread is created. An efficient way to determine CPU usage is to use the ‘Processor:% Processor Time’ counter in System Monitor. This counter monitors the amount of time the CPU spends executing a thread that is not idle. Using the GetCounterInstances by providing counter path and string list.
  • For each cores created a label mentioning Core number and a TGauge to represent the performance.
Код:
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  lbl: TLabel;
  g: TGauge;
begin
  sl:=TStringList.Create;
  FPM:=TPerfMonThread.Create;
  cPC:=cPC1;
  FPM.GetCounterInstances(cPC,sl);
  if sl.Count=0 then begin
    cPC:=cPC2;
    FPM.GetCounterInstances(cPC,sl);
  end;
  FPM.OnInterval:=PerfMonThreadInterval;
  FPM.AddCounter(cPC);
  for i:=0 to sl.Count-1 do begin
    lbl:=TLabel.Create(Self);
    with lbl do begin
      Parent:=Self;
      if SameText(sl[i],'_Total') then
        Caption:='Total'
      else
        Caption:='Core '+sl[i];
      Top:=(Height+8)*i+10;
      Left:=10;
    end;
    g:=TGauge.Create(Self);
    g.Name:=Format('Gauge%d',[i]);
    g.Parent:=Self;
    g.Top:=lbl.Top;
    g.Left:=90;
    g.Height:=lbl.Height+2;
    g.Width:=Self.ClientWidth-g.Left-10;
    if SameText(sl[i],'_Total') then
      g.ForeColor:=$000EC9FF
    else if Pos(',_Total',sl[i])>0 then
      g.ForeColor:=$0000DDDD
    else
      g.ForeColor:=$0031D329;
  end;
 
  if sl.Count=0 then
    lWarn.Show
  else begin
    Self.ClientHeight:=lbl.Top+lbl.Height*2;
    FPM.Suspended:=False;
  end;
  Update;
end;
  • On each interval, the progress of the Gauge is updated by ReadCounter property.
Код:
procedure TForm1.PerfMonThreadInterval(Sender: TPerfMonThread);
var
  i: Integer;
  g: TGauge;
begin
  for i:=0 to sl.Count-1 do begin
    g:=TGauge(FindComponent(Format('Gauge%d',[i])));
    if Assigned(g) then
      g.Progress:=Round(Sender.ReadCounter(FastStringReplace(cPC,'*',sl[i])));
  end;
end;
1612855476784.png
MiTeC CPU Usage Demo

It’s that simple to monitor CPU usage in your machine and view the performance of the CPU cores. Use this MiTeC component suite and get the job done with less code.

Для просмотра ссылки Войди или Зарегистрируйся