当前位置:首页 > 网络编程 > 软件语言 > DELPHI > 如何取得一台机器的CPU占用率

如何取得一台机器的CPU占用率

点击次数:61 次 发布日期:2008-11-09 08:42:07 作者:源代码网
源代码网推荐
广告载入中
使用下面的方法
源代码网推荐 interface
源代码网推荐
源代码网推荐 uses
源代码网推荐 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
源代码网推荐 Dialogs, StdCtrls;
源代码网推荐
源代码网推荐 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; {LARGE_INTEGER}
源代码网推荐 dwSpare: array[0..75] of DWORD;
源代码网推荐 end;
源代码网推荐
源代码网推荐 type
源代码网推荐 TSystem_Time_Information = packed record
源代码网推荐 liKeBootTime: LARGE_INTEGER;

软件开发网 www.mscto.com


源代码网推荐 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 = ();
源代码网推荐 SysBaseInfo: TSystem_Basic_Information;
源代码网推荐 SysPerfInfo: TSystem_Performance_Information;
源代码网推荐 SysTimeInfo: TSystem_Time_Information;
源代码网推荐 status: Longint; {long}
源代码网推荐 dbSystemTime: Double;
源代码网推荐 dbIdleTime: Double;
源代码网推荐 function GetCPUUsage:Double;
源代码网推荐 implementation
源代码网推荐 function Li2Double(x: LARGE_INTEGER): Double;
源代码网推荐 begin
源代码网推荐 Result := x.HighPart * 4.294967296E9 x.LowPart
源代码网推荐 end;
源代码网推荐
源代码网推荐 function GetCPUUsage:Double;
源代码网推荐 var
源代码网推荐 bLoopAborted : boolean;
源代码网推荐 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 Exit;
源代码网推荐 // Show some information
源代码网推荐 {with SysBaseInfo do
源代码网推荐 begin
源代码网推荐 ShowMessage(
源代码网推荐 Format(‘uKeMaximumIncrement: %d‘#13‘uPageSize: %d‘#13
源代码网推荐 ‘uMmNumberOfPhysicalPages: %d‘ #13 ‘uMmLowestPhysicalPage: %d‘ #13
源代码网推荐 ‘uMmHighestPhysicalPage: %d‘ #13 ‘uAllocationGranularity: %d‘#13
源代码网推荐 ‘uKeActiveProcessors: %d‘#13‘bKeNumberProcessors: %d‘,
源代码网推荐 [uKeMaximumIncrement, uPageSize, uMmNumberOfPhysicalPages,
源代码网推荐 uMmLowestPhysicalPage, uMmHighestPhysicalPage, uAllocationGranularity,
源代码网推荐 uKeActiveProcessors, bKeNumberProcessors]));
源代码网推荐 end;
源代码网推荐 }
源代码网推荐 bLoopAborted := False;
源代码网推荐 while not bLoopAborted do
源代码网推荐 begin
源代码网推荐 // get new system time
源代码网推荐 status := NtQuerySystemInformation(SystemTimeInformation, @SysTimeInfo, SizeOf(SysTimeInfo), 0);
源代码网推荐 if status <> 0 then Exit;
源代码网推荐 // get new CPU‘s idle time
源代码网推荐 status := NtQuerySystemInformation(SystemPerformanceInformation, @SysPerfInfo, SizeOf(SysPerfInfo), nil);

源代码网推荐 if status <> 0 then Exit;
源代码网推荐 // 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;
源代码网推荐 // Show Percentage
源代码网推荐 //Form1.Label1.Caption := FormatFloat(‘CPU Usage: 0.0 %‘,dbIdleTime);
源代码网推荐 //Application.ProcessMessages;
源代码网推荐 // Abort if user pressed ESC or Application is terminated
源代码网推荐 Result:=dbIdleTime;
源代码网推荐 bLoopAborted:=True;
源代码网推荐 //bLoopAborted := (GetKeyState(VK_ESCAPE) and 128 = 128) or Application.Terminated;
源代码网推荐 end;
源代码网推荐 // store new CPU‘s idle and


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华