Monitoring CPU usage becomes an essential use case in some of the applications. Don’t know how to monitor the CPU usage in your Delphi Applications? Don’t worry. MiTec’s System Information Management Suite’s component helps 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.
- 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.
- 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.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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.
1 2 3 4 5 6 7 8 9 10 11 |
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; |
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.
With Windows IDE, you can easily monitor CPU usage from your Delphi Windows apps with a component suite. Try your Free Trial here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
How do you monitor other computers on the network?
If you go to the Mitec site and look for the “monitor” demos there are examples there of how to monitor both local and networked/remote machines. The link is here: https://www.mitec.cz/msics.html