delphi 获取当前进程的cpu占用率
2024-10-02 01:55:12
type
TProcessCpuUsage = record
private
FLastUsed, FLastTime: Int64;
FCpuCount:Integer;
public
class function Create: TProcessCpuUsage; static;
function Current: Single;
end;
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;
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;
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;
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
http://blog.csdn.net/y281252548/article/details/50600028
最新文章
- 向ES6靠齐的Class.js
- SIP模块版本错误问题:the sip module implements API v??? but XXX module requires API v???
- 磁盘、分区及Linux文件系统 [Disk, Partition, Linux File System]
- [MSSQL2012]CUME_DIST函数
- 简述Mesos API–files
- Objective-C:Foundation框架-常用类-NSDate
- 关于获取目录的N种方法 的汇总
- Linux 2.4.x内核软中断机制
- FIREDAC调用中间件远程方法查询数据示例
- MySQL与NoSQL——SQL与NoSQL的融合
- mysql-5.6.27源码安装及错误解决办法
- JS对象创建常用方式及原理分析
- DevExpress控件安装破解和汉化使用教程
- codeblock 恢复默认字体设置
- Maven学习 五 Maven项目创建(1)jar项目
- jquery之源码
- Java核心技术之基础知识
- sencha touch list(列表) item(单行)单击事件触发顺序
- [py]django上线部署-uwsgi+nginx+py3/django1.10
- 获取字符串中某个指定的子串出现的开始位置(CHARINDEX用法)