使用Delphi将内存使用放在TProgressBar.position中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Delphi将内存使用放在TProgressBar.position中相关的知识,希望对你有一定的参考价值。

我想知道如何使用正在使用的计算机的内存使用量并将数量放入TProgressBar组件中。

我知道可以将CPU使用率放在那里,但我不知道如何使用Memory。我该怎么做呢?

答案

我想我得到了你需要的东西。

unit RamMonitor;

interface

uses
  Classes, Windows, SyncObjs;

type
  TOnRefresh = Procedure(RAMUsage: Double) of object;
  TOnRefreshExt = Procedure(RAMUsage: Double; RAMTotal, RAMFree, RAMUsed: Integer) of object;

type
  TRAMMonitor = class(TThread)
    private
      FPauseEvent : TEvent;
      FOnRefresh: TOnRefresh;
      FOnRefreshExt: TOnRefreshExt;

      FRefreshInterval: Integer;
      FRAMUsage: Double;
      FRAMTotal: Integer;
      FRAMFree: Integer;
      FRAMUsed: Integer;

      procedure ReportRAMUsage;
      procedure CheckForPause;
    public
      property RefreshInterval : Integer read FRefreshInterval write FRefreshInterval;
      property OnRefresh: TOnRefresh read FOnRefresh write FOnRefresh;
      property OnRefreshExt: TOnRefreshExt read FOnRefreshExt write FOnRefreshExt;

      constructor Create; reintroduce;
      destructor Destroy; override;
      procedure Pause;
      procedure UnPause;
    protected
      procedure Execute; override;
  end;

const
  ONEMEGABYTE   = 1024 * 1024;
  INTERVAL      = 5000; //ms

implementation

constructor TRAMMonitor.Create;
begin
  inherited Create(true);

  FPauseEvent := TEvent.Create(nil, true, true, '');
  FRefreshInterval := INTERVAL;
end;

destructor TRAMMonitor.Destroy;
begin
  if not Terminated then
    Terminate;
  FPauseEvent.Free;
  inherited;
end;

procedure TRAMMonitor.CheckForPause;
begin
  FPauseEvent.WaitFor(INFINITE);
end;

procedure TRAMMonitor.Pause;
begin
  FPauseEvent.ResetEvent;
end;

procedure TRAMMonitor.UnPause;
begin
  FPauseEvent.SetEvent;
end;

procedure TRAMMonitor.Execute;
var
  x, y : real;
  memory: Windows.TMemoryStatus;
begin
  repeat
    CheckForPause;
    // memory
    memory.dwLength := SizeOf(memory);
    GlobalMemoryStatus(memory);
    x := memory.dwTotalPhys - memory.dwAvailPhys;
    y := memory.dwTotalPhys;
    FRAMUsage := ((x/y)*100);
    FRAMTotal := round(memory.dwTotalPhys/ONEMEGABYTE);
    FRAMFree := round(memory.dwAvailPhys/ONEMEGABYTE);
    FRAMUsed := round((memory.dwTotalPhys - memory.dwAvailPhys)/ONEMEGABYTE);

    Synchronize(ReportRAMUsage);
    sleep(FRefreshInterval);
  until (Terminated);
end;

procedure TRAMMonitor.ReportRAMUsage;
begin
  if Assigned(OnRefresh) then
    OnRefresh(FRAMUsage);

  if Assigned(OnRefreshExt) then
    OnRefreshExt(FRAMUsage, FRAMTotal, FRAMFree, FRAMUsed);
end;

end.

在你的主要的初始代码中:

FRAMUsage := TRamMonitor.Create;
FRAMUsage.RefreshInterval := 15000; // 15s
FRAMUsage.OnRefresh := RefreshRamUsage;
FRAMUsage.Unpause; // in order to make the thread run

这里是刷新过程,在这个例子中,用法是百分比,所以你需要将max设置为100.第二个事件(在这个例子中没有使用)提供了有关内存使用量的更多细节,以兆字节为单位。

procedure TfrmMain.RefreshRamUsage(Usage: Double);
var
  NewPos : Integer;
begin
  NewPos := Trunc(Usage);
  pbRamUsage.Position := NewPos;
end;

心连心

以上是关于使用Delphi将内存使用放在TProgressBar.position中的主要内容,如果未能解决你的问题,请参考以下文章

如何获取 Delphi 程序使用的内存

如何使用在设计时获取“无法将 NIL 分配给 TFont”的 TFont 属性修复 Delphi 组件?

Delphi 的探查器和内存分析工具 [关闭]

Delphi:如何在公共函数中使用 ComponentCount?

delphi 如何用ssleay32.dll

关于Delphi内存表的使用说明