怎样得到CPU的利用率!

解决方案 »

  1.   

    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.
      

  2.   

    取得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;