请各位高手帮忙: 怎样得到CPU的利用率!在线等等...... 怎样得到CPU的利用率! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, CommCtrl, StdCtrls, Menus,WinSpool, ExtCtrls, Validat, Buttons, Registry;type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; Label2: TLabel; Timer1: TTimer; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private-Deklarationen } started : boolean; reg : TRegistry; public { Public-Deklarationen } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject); var Dummy : array[0..1024] of byte;begin Reg:=TRegistry.Create; Reg.RootKey:=HKEY_DYN_DATA; //统计数据在这个表项下 Reg.OpenKey('PerfStats\StartStat',false); // Reg.ReadBinaryData('KERNEL\CPUUsage',Dummy,Sizeof(Dummy)); Reg.CloseKey; started:=true;end;procedure TForm1.Timer1Timer(Sender: TObject); var CPUU : integer;begin if started then begin Reg.OpenKey('PerfStats\StatData',false); Reg.ReadBinaryData('KERNEL\CPUUsage',CPUU,SizeOf(Integer)); Reg.CloseKey; Label1.Caption:=IntToStr(CPUU)+'%'; end;end;procedure TForm1.Button2Click(Sender: TObject); var Dummy : array[0..1024] of byte;begin'PerfStats/StopStat' } Reg.OpenKey('PerfStats\StopStat',false); Reg.ReadBinaryData('KERNEL\CPUUsage',Dummy,SizeOf(Dummy)); Reg.Free; Started:=false;end;end. 取得CPU的使用率 http://www.swissdelphicenter.ch/torry/showcode.php?id=969 How to get the CPU usage in percentconstSystemBasicInformation = 0;SystemPerformanceInformation = 2;SystemTimeInformation = 3;typeTPDWord = ^DWORD;TSystem_Basic_Information = packed recorddwUnknown1: 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;typeTSystem_Performance_Information = packed recordliIdleTime: LARGE_INTEGER; {LARGE_INTEGER}dwSpare: array[0..75] of DWORD;end;typeTSystem_Time_Information = packed recordliKeBootTime: LARGE_INTEGER;liKeSystemTime: LARGE_INTEGER;liExpTimeZoneBias: LARGE_INTEGER;uCurrentTimeZoneId: ULONG;dwReserved: DWORD;end;varNtQuerySystemInformation: function(infoClass: DWORD;buffer: Pointer;bufSize: DWORD;returnSize: TPDword): DWORD; stdcall = nil; liOldIdleTime: LARGE_INTEGER = ();liOldSystemTime: LARGE_INTEGER = ();function Li2Double(x: LARGE_INTEGER): Double;beginResult := x.HighPart * 4.294967296E9 + x.LowPartend;procedure GetCPUUsage;varSysBaseInfo: TSystem_Basic_Information;SysPerfInfo: TSystem_Performance_Information;SysTimeInfo: TSystem_Time_Information;status: Longint; {long}dbSystemTime: Double;dbIdleTime: Double;bLoopAborted : boolean;beginif @NtQuerySystemInformation = nil thenNtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'),'NtQuerySystemInformation');// get number of processors in the systemstatus := NtQuerySystemInformation(SystemBasicInformation, @SysBaseInfo, SizeOf(SysBaseInfo), nil);if status <> 0 then Exit;// Show some informationwith SysBaseInfo dobeginShowMessage(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 dobegin// get new system timestatus := NtQuerySystemInformation(SystemTimeInformation, @SysTimeInfo, SizeOf(SysTimeInfo), 0);if status <> 0 then Exit;// get new CPU's idle timestatus := NtQuerySystemInformation(SystemPerformanceInformation, @SysPerfInfo, SizeOf(SysPerfInfo), nil);if status <> 0 then Exit;// if it's a first call - skip itif (liOldIdleTime.QuadPart <> 0) thenbegin// CurrentValue = NewValue - OldValuedbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);// CurrentCpuIdle = IdleTime / SystemTimedbIdleTime := dbIdleTime / dbSystemTime;// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessorsdbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5;// Show PercentageForm1.Label1.Caption := FormatFloat('CPU Usage: 0.0 %',dbIdleTime);Application.ProcessMessages;// Abort if user pressed ESC or Application is terminatedbLoopAborted := (GetKeyState(VK_ESCAPE) and 128 = 128) or Application.Terminated;end;// store new CPU's idle and system timeliOldIdleTime := SysPerfInfo.liIdleTime;liOldSystemTime := SysTimeInfo.liKeSystemTime;// wait one secondSleep(1000);end;end; procedure TForm1.Button1Click(Sender: TObject);beginGetCPUUsageend; 单击form2中的一个按钮,显示form1窗体的代码怎么写? 请问如何取得正在编辑中的word文档的句柄? 再线提问:在下是菜鸟,请问各位高手:抢先windows登陆都需调用那些api? DELPHI作为一种通用的软件开发工具有哪些主要的优点和不足 那个函数是可以获得String的长度的啊 通过流将图象写入数据库中,下面的代码有什么错误? 高手,请帮忙!问题可能不是很难? Delphi部件开发编程深入剖析 这本书怎么样? 今天下载了indy控件,请问哪里有indy的DEMO啊? 关于readfile(...)的问题 请问如何将字符串按一定格式分开 高手解决一下DBGridEh问题!
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, CommCtrl, StdCtrls, Menus,WinSpool, ExtCtrls, Validat, Buttons,
Registry;type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private-Deklarationen }
started : boolean;
reg : TRegistry;
public
{ Public-Deklarationen }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
Dummy : array[0..1024] of byte;begin
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_DYN_DATA; //统计数据在这个表项下
Reg.OpenKey('PerfStats\StartStat',false); // Reg.ReadBinaryData('KERNEL\CPUUsage',Dummy,Sizeof(Dummy));
Reg.CloseKey;
started:=true;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
CPUU : integer;begin
if started then
begin
Reg.OpenKey('PerfStats\StatData',false);
Reg.ReadBinaryData('KERNEL\CPUUsage',CPUU,SizeOf(Integer));
Reg.CloseKey;
Label1.Caption:=IntToStr(CPUU)+'%';
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
Dummy : array[0..1024] of byte;begin
'PerfStats/StopStat' }
Reg.OpenKey('PerfStats\StopStat',false);
Reg.ReadBinaryData('KERNEL\CPUUsage',Dummy,SizeOf(Dummy));
Reg.Free;
Started:=false;
end;end.
http://www.swissdelphicenter.ch/torry/showcode.php?id=969
How to get the CPU usage in percentconstSystemBasicInformation = 0;SystemPerformanceInformation = 2;SystemTimeInformation = 3;typeTPDWord = ^DWORD;TSystem_Basic_Information = packed recorddwUnknown1: 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;typeTSystem_Performance_Information = packed recordliIdleTime: LARGE_INTEGER; {LARGE_INTEGER}dwSpare: array[0..75] of DWORD;end;typeTSystem_Time_Information = packed recordliKeBootTime: LARGE_INTEGER;liKeSystemTime: LARGE_INTEGER;liExpTimeZoneBias: LARGE_INTEGER;uCurrentTimeZoneId: ULONG;dwReserved: DWORD;end;varNtQuerySystemInformation: function(infoClass: DWORD;buffer: Pointer;bufSize: DWORD;returnSize: TPDword): DWORD; stdcall = nil; liOldIdleTime: LARGE_INTEGER = ();liOldSystemTime: LARGE_INTEGER = ();function Li2Double(x: LARGE_INTEGER): Double;beginResult := x.HighPart * 4.294967296E9 + x.LowPartend;procedure GetCPUUsage;varSysBaseInfo: TSystem_Basic_Information;SysPerfInfo: TSystem_Performance_Information;SysTimeInfo: TSystem_Time_Information;status: Longint; {long}dbSystemTime: Double;dbIdleTime: Double;bLoopAborted : boolean;beginif @NtQuerySystemInformation = nil thenNtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'),'NtQuerySystemInformation');// get number of processors in the systemstatus := NtQuerySystemInformation(SystemBasicInformation, @SysBaseInfo, SizeOf(SysBaseInfo), nil);if status <> 0 then Exit;// Show some informationwith SysBaseInfo dobeginShowMessage(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 dobegin// get new system timestatus := NtQuerySystemInformation(SystemTimeInformation, @SysTimeInfo, SizeOf(SysTimeInfo), 0);if status <> 0 then Exit;// get new CPU's idle timestatus := NtQuerySystemInformation(SystemPerformanceInformation, @SysPerfInfo, SizeOf(SysPerfInfo), nil);if status <> 0 then Exit;// if it's a first call - skip itif (liOldIdleTime.QuadPart <> 0) thenbegin// CurrentValue = NewValue - OldValuedbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);// CurrentCpuIdle = IdleTime / SystemTimedbIdleTime := dbIdleTime / dbSystemTime;// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessorsdbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5;// Show PercentageForm1.Label1.Caption := FormatFloat('CPU Usage: 0.0 %',dbIdleTime);Application.ProcessMessages;// Abort if user pressed ESC or Application is terminatedbLoopAborted := (GetKeyState(VK_ESCAPE) and 128 = 128) or Application.Terminated;end;// store new CPU's idle and system timeliOldIdleTime := SysPerfInfo.liIdleTime;liOldSystemTime := SysTimeInfo.liKeSystemTime;// wait one secondSleep(1000);end;end; procedure TForm1.Button1Click(Sender: TObject);beginGetCPUUsageend;