type
TProcessCpuUsage = record
private
FLastUsed, FLastTime: Int64;
FCpuCount:Integer;
public
class function Create: TProcessCpuUsage; static;
function Current: Single;
end;
var
ProcessCpuUsage: TProcessCpuUsage = (FLastUsed: 0; FLastTime: 0;FCpuCount:0);
class function TProcessCpuUsage.Create: TProcessCpuUsage;
begin
Result.FLastTime := 0;
Result.FLastUsed := 0;
Result.FCpuCount := 0;
end;
function TProcessCpuUsage.Current: Single;
var
Usage, ACurTime: UInt64;
CreateTime, ExitTime, IdleTime, UserTime, KernelTime: TFileTime;
function FileTimeToI64(const ATime: TFileTime): Int64;
begin
Result := (Int64(ATime.dwHighDateTime) shl 32) + ATime.dwLowDateTime;
end;
function GetCPUCount: Integer;
var
SysInfo: TSystemInfo;
begin
GetSystemInfo(SysInfo);
Result := SysInfo.dwNumberOfProcessors;
end;
begin
Result := 0;
if GetProcessTimes(GetCurrentProcess, CreateTime, ExitTime, KernelTime,
UserTime) then
begin
ACurTime := GetTickCount;
Usage := FileTimeToI64(UserTime) + FileTimeToI64(KernelTime);
if FLastTime <> 0 then
Result := (Usage - FLastUsed) / (ACurTime - FLastTime) /
FCpuCount / 100
else
FCpuCount:=GetCpuCount;
FLastUsed := Usage;
FLastTime := ACurTime;
end;
end;
if ProcessCpuUsage.Current >= 25 then
begin
............................
end