【问题标题】:Put the Memory Usage in a TProgressBar.position with Delphi使用 Delphi 将内存使用情况放在 TProgressBar.position
【发布时间】:2014-10-23 13:19:16
【问题描述】:

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

我知道可以获取 CPU 使用率并将其放在那里,但我不知道如何使用内存。我该怎么做?

【问题讨论】:

标签: windows delphi memory vcl


【解决方案1】:

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

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.

在你的 main 的初始化代码中:

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

这里是刷新过程,在这个例子中,使用是百分比,所以你需要将最大值设置为 100。 第二个事件(本示例中未使用)提供有关内存使用量的更多详细信息(以 MB 为单位)。

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

【讨论】:

  • 没有更简单的方法吗?
  • 我正在寻找一种快速简便的方法。
  • 谢谢。我从 2015 年开始看这个问题,现在我喜欢你的回答。谢谢你。我会在这周的某个时候对其进行测试。
猜你喜欢
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 2012-05-30
  • 2021-02-26
相关资源
最近更新 更多