博客
关于我
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/

你可能感兴趣的文章
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
Orleans框架------基于Actor模型生成分布式Id
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>
orm总结
查看>>
os.environ 没有设置环境变量
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.removexattr 的 Python 文档——‘*‘(星号)参数是什么意思?
查看>>
os.system 在 Python 中不起作用
查看>>
OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
查看>>
OSCACHE介绍
查看>>