博客
关于我
delphi 获取当前进程的cpu占用率
阅读量:734 次
发布时间:2019-03-21

本文共 771 字,大约阅读时间需要 2 分钟。

TProcessCpuUsage类用于监控CPU使用情况,该类由两部分组成: Create方法初始化数据,Current方法返回当前CPU使用率。以下是类的详细说明:

类TProcessCpuUsage:

  • FLastUsed记录上一次使用时间
  • FLastTime记录上一次监测时间
  • FCpuCount记录CPU核心数量

class function Create:

  • 返回新实例的TProcessCpuUsage
  • 初始化FLastUsed为0
  • 初始化FLastTime为0
  • 初始化FCpuCount为0

function Current:

  • 返回当前CPU使用率(单值)
  • 获取目前系统时间ACurTime
  • 调用FileTimeToI64函数将KernelTime和UserTime转换为UInt64
  • 计算Usage总和
  • 比较FLastTime和FLastUsed,计算CPU使用率
  • 如果FLastTime>0,计算平均使用率并分配到FCpuCount
  • 否则,使用GetCPUCount获取当前CPU数量
  • 更新FLastUsed为当前Usage
  • 更新FLastTime为当前ACurTime

function GetCPUCount:

  • 调用GetSystemInfo函数获取SysInfo.dwNumberOfProcessors
  • 返回CPU核心数量

方法实现:

  • FileTimeToI64函数将TFileTime转换为Int64
  • GetSystemInfo函数获取系统信息
  • GetTickCount函数获取系统时间

注意事项:

  • 使用ProcessCpuUsage的Current值进行判断:ProcessCpuUsage.Current >=25则触发相应逻辑

该类适用于个人项目或需要轻量级CPU性能监控的场景,适合在Windows系统中使用。

转载地址:http://mfegz.baihongyu.com/

你可能感兴趣的文章
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
node+express+mysql 实现登陆注册
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Slider滑杆和Numeric数值输入组件的使用
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>
Node-RED中使用exec节点实现调用外部exe程序
查看>>