DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: aaaarrrrrrrrrrrr
今日帖子: 59
在线用户: 8
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 acheng_wh (acheng) ★☆☆☆☆ -
盒子活跃会员
2004/2/25 9:25:19
标题:
怎么获取CPU的使用率? 浏览:1469
加入我的收藏
楼主: 可以通过以下方法获取内存等的使用信息
procedure TForm1.Button1Click(Sender: TObject);
var MemInfo: MEMORYSTATUS;
begin
  // 获取内存信息
  GlobalMemoryStatus(MemInfo);
  Memo1.Lines.Add(IntToStr(MemInfo.dwMemoryLoad) + '%的内存在使用');
  Memo1.Lines.Add('物理内存共有' + IntToStr(MemInfo.dwTotalPhys) + '字节');
  Memo1.Lines.Add('未使用的物理内存有' + IntToStr(MemInfo.dwAvailPhys) + '字节');
  Memo1.Lines.Add('交换文件的大小为' + IntToStr(MemInfo.dwTotalPageFile) + '字节');
  Memo1.Lines.Add('未使用的交换文件大小为' + IntToStr(MemInfo.dwAvailPageFile) + '字节');
  Memo1.Lines.Add('虚拟内存空间大小为' + IntToStr(MemInfo.dwTotalVirtual) + '字节');
  Memo1.Lines.Add('未使用的虚拟内存大小为' + IntToStr(MemInfo.dwAvailVirtual) + '字节');
end;



但怎么获取CPU的使用率?谢谢!
----------------------------------------------
-
作者:
男 bios (阿贡) ★☆☆☆☆ -
盒子中级会员
2004/2/25 11:34:00
1楼: http://bbs.2ccc.com/topic.asp?topicid=62848
----------------------------------------------
按此在新窗口浏览图片
按此在新窗口浏览图片
作者:
男 acheng_wh (acheng) ★☆☆☆☆ -
盒子活跃会员
2004/2/25 14:15:49
2楼: 从别的地方抄来的

//-------------获得CPU使用信息-------------------------------------------
const
    SystemBasicInformation = 0;
    SystemPerformanceInformation = 2;
    SystemTimeInformation = 3;
type
    TPDWord = ^DWORD;
    TSystem_Basic_Information = packed record
        dwUnknown1: DWORD;
        uKeMaximumIncrement: ULONG;
        uPageSize: ULONG;
        uMmNumberOfPhysicalPages: ULONG;
        uMmLowestPhysicalPage: ULONG;
        uMmHighestPhysicalPage: ULONG;
        uAllocationGranularity: ULONG;
        pLowestUserAddress: Pointer;
        pMmHighestUserAddress: Pointer;
        uKeActiveProcessors: ULONG;
        bKeNumberProcessors: byte;
        bUnknown2: byte;
        wUnknown3: word;
    end;
type
    TSystem_Performance_Information = packed record
        liIdleTime: LARGE_INTEGER;
        dwSpare: array[0..75] of DWORD;
    end;
type
    TSystem_Time_Information = packed record
        liKeBootTime: LARGE_INTEGER;
        liKeSystemTime: LARGE_INTEGER;
        liExpTimeZoneBias: LARGE_INTEGER;
        uCurrentTimeZoneId: ULONG;
        dwReserved: DWORD;
    end;

var
    NtQuerySystemInformation: function(infoClass: DWORD;
        buffer: Pointer;
        bufSize: DWORD;
        returnSize: TPDword): DWORD; stdcall = nil;
    liOldIdleTime: LARGE_INTEGER = ();
    liOldSystemTime: LARGE_INTEGER = ();

function Li2Double(x: LARGE_INTEGER): Double;
begin
    Result := x.HighPart * 4.294967296E9 + x.LowPart
end;

function GetCPUUsage(): Double;
var
    SysBaseInfo: TSystem_Basic_Information;
    SysPerfInfo: TSystem_Performance_Information;
    SysTimeInfo: TSystem_Time_Information;
    status: Longint;
    dbSystemTime: Double;
    dbIdleTime: Double;

begin
    if @NtQuerySystemInformation = nil then
        NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'),
            'NtQuerySystemInformation');
    // get number of processors in the system
    status := NtQuerySystemInformation(SystemBasicInformation, @SysBaseInfo,
        SizeOf(SysBaseInfo), nil);
    if status <> 0 then
        Result := 0.5;

    // get new system time
    status := NtQuerySystemInformation(SystemTimeInformation,
        @SysTimeInfo, SizeOf(SysTimeInfo), nil); //nil
    if status <> 0 then
        Result := 0.5;

    // get new CPU's idle time
    status := NtQuerySystemInformation(SystemPerformanceInformation,
        @SysPerfInfo, SizeOf(SysPerfInfo), nil);
    if status <> 0 then
        Result := 0.5;

    // if it's a first call - skip it
    if (liOldIdleTime.QuadPart <> 0) then
        begin
            // CurrentValue = NewValue - OldValue
            dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) -
                Li2Double(liOldIdleTime);
            dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) -
                Li2Double(liOldSystemTime);
            // CurrentCpuIdle = IdleTime / SystemTime
            dbIdleTime := dbIdleTime / dbSystemTime;
            // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
            dbIdleTime := 100.0 - dbIdleTime * 100.0 /
                SysBaseInfo.bKeNumberProcessors + 0.5;
            // Result
            Result := dbIdleTime;
            //Application.ProcessMessages;
        end
    else // store new CPU's idle and system time
        begin
            liOldIdleTime := SysPerfInfo.liIdleTime;
            liOldSystemTime := SysTimeInfo.liKeSystemTime;
            Result := 0.5;
        end;
end;
//--------------------完成获得CPU使用信息--------------------------------


不知怎样!
----------------------------------------------
-
作者:
男 bios (阿贡) ★☆☆☆☆ -
盒子中级会员
2004/2/25 14:51:20
3楼: 不知道 你自己试吧
----------------------------------------------
按此在新窗口浏览图片
按此在新窗口浏览图片
作者:
男 bbsxwk (can) ★☆☆☆☆ -
盒子活跃会员
2004/3/28 4:03:50
4楼: 很早就用过了
就是编译不出来
手头只有几个VB的
不知道DELPHI怎么实现啊:(
----------------------------------------------
<<依稀往梦似曾见,心内波澜现...>>
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行406.25毫秒 RSS