To the main page...The list of my products...Some texts...Sample applications, tips, tricks...If you need support...
 

 
CPU usage
Published 27.06.2000
It is compatible with... It uses...
Not Win 95 compatible Not Win 98 compatible Win NT compatible Windows 2000 compatible Delphi 3 compatible Delphi 4 compatible Delphi 5 compatibleC++ Builder 3 CompatibleC++ Builder 4 Compatible  PerfUtils components are necessary
The complete source code as well as compiled, ready for tests version of this example is available here (zip, 165k, Delphi version).

I have been asked many times about this example. One guy have even promised to pay me for this example (but he has not ;) ). I considered this example to be too simple for the demos section of my site but finally I decided to form it and place here.

Well, let's do it.

Create new project and place the following components onto it's surface:

  • TPerfMonitor component - to gather performance data;
  • TTimer - to refresh the data;
  • TProgressBar - to show the result;

The resulting form should look like this one:

CPU Usage form

Now the PerfMonitor component should be configured. The first necessary operation is setting the LocaleId property. This property defines the language of performance object's and counter's titles. Set this property to 009. It means English, and this value is valid for localized versions of Windows NT/2000 too:

Setting LocaleId property

Now adjust the Items property of this component. This property contains a collection of monitoring items. Add one item to it and set it's properties.

At first set the ObjectName to "Processor". The drop-down list contains the names of all performance objects existing on your system and you could choose the object name from this list.

Now you can set the value of CounterName property. This property has drop-down list of possible items too. It contains the names of all counters that are defined for the performance object specified by ObjectName property.

Monitoring item properties

Well, the monitoring item is configured properly now. Add the following code to the Timer.OnTimer event handler:




 
 
 
 

 


 


 

 


procedure TMainForm.TimerTimer(Sender: TObject);
begin
    if _Busy then exit;
    _Busy := True; // Do not allow re-entering the refresh loop.
    try
        // Gather performance data...
        PM.Collect;

        // ...and show it!
        PB.Position:=Round(PM.Items[0].InstanceValues[0].AsFloat);

    finally
        _Busy := False;
    end;
end;

Only one comment is necessary here. The "Processor" performance object is multi-instance because there could be more then one processor in the system. So the indexed property InstanceValues must be used to obtain the counter value. In our case it will be the CPU usage value.

And one more thing. The _Busy boolean variable is used to prevent re-entering the refreshing loop. Add the declaration of this variable to the private section of form's declaration.

Now our example is ready for tests. Save it, compile it, run it and enjoy!

Add your comment | Read comments


 
© 1998-2001 Alexey Dynnikov
My ICQ # is 18267212