Delphi:框架 TList 的问题

发布于 2024-10-11 09:54:57 字数 1036 浏览 7 评论 0原文

我遇到了一个问题,该界面由 TScrollBox 中的多个框架(通常为 25 个)组成。

有 2 个问题,我希望其中一个是另一个问题的结果...

背景:

当应用程序启动时,我创建 25 个帧,每个帧包含大约 1 个帧。 20 个控件,然后填充默认信息。然后,用户可以单击控件将搜索限制为信息子集,此时我可以释放并重新创建我的框架(因为搜索可能返回 <25 条记录)

问题:

如果我退出初始搜索后的应用程序大约需要5秒返回Delphi。在第二次搜索(并处理/重新创建帧)之后,大约需要花费时间。 20 秒)

虽然我可以重写应用程序以仅创建一次框架,但我想了解发生了什么。

这是我的创建例程:

procedure TMF.CreateFrame(i: Integer; var FrameBottom: Integer);
var
   NewFrame: TSF;
begin
   NewFrame := TSF.Create(Self);
   NewFrame.Name := 'SF' + IntToStr(i);
   if i = 0 then
      NewSF.Top := 8
   else
      NewSF.Top := FrameBottom + 8;
   FrameBottom := NewFrame.Top + NewFrame.Height;
   NewFrame.Parent := ScrollBox1;
   FrameList.Add(NewFrame);
end;

这是我的删除例程:

procedure TMF.ClearFrames;
var
   i: Integer;
   SF: TSF;
begin
   for i := 0 to MF.FrameList.Count -1  do
   begin
      SF := FrameList[i];
      SF.Free;
   end;
   FrameList.Clear;
end;

我缺少什么?

I'm having a problem with an interface that consists of a number of frames (normally 25) within a TScrollBox.

There are 2 problems, and I am hoping that one is a consequence of the other...

Background:

When the application starts up, I create 25 frames, each containing approx. 20 controls, which are then populated with the default information. The user can then click on a control to limit the search to a subset of information at which point I free and recreate my frames (as the search may return < 25 records)

The problem:

If I quit the application after the initial search then it takes approx. 5 seconds to return to Delphi. After the 2nd search (and dispose / recreate of frames) it takes approx. 20 seconds)

Whilst I could rewrite the application to only create the frames once, I would like to understand what is going on.

Here is my create routine:

procedure TMF.CreateFrame(i: Integer; var FrameBottom: Integer);
var
   NewFrame: TSF;
begin
   NewFrame := TSF.Create(Self);
   NewFrame.Name := 'SF' + IntToStr(i);
   if i = 0 then
      NewSF.Top := 8
   else
      NewSF.Top := FrameBottom + 8;
   FrameBottom := NewFrame.Top + NewFrame.Height;
   NewFrame.Parent := ScrollBox1;
   FrameList.Add(NewFrame);
end;

And here is my delete routine:

procedure TMF.ClearFrames;
var
   i: Integer;
   SF: TSF;
begin
   for i := 0 to MF.FrameList.Count -1  do
   begin
      SF := FrameList[i];
      SF.Free;
   end;
   FrameList.Clear;
end;

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-10-18 09:54:57

由于您通过释放正在创建的框架来控制其内存分配,因此无需在创建构造函数中提供 Self 作为所有者参数。传递 nil 来防止所有者尝试释放框架。

另外,不喜欢 ClearFrames 例程的外观。试试这个:

while FrameList.count > 0 do
begin
    TSF(Framelist[0]).free;
    Framelist.delete(0);
end;
Framelist.clear;

As you are taking control over the memory allocation of the Frames you are creating by Free'ing them, so there's no need to provide Self as the owner parameter in the create constructor. Pass nil instead to prevent the owner trying to free the frame.

Also, don't like the look of your ClearFrames routine. Try this instead:

while FrameList.count > 0 do
begin
    TSF(Framelist[0]).free;
    Framelist.delete(0);
end;
Framelist.clear;
通知家属抬走 2024-10-18 09:54:57

如果您想知道为什么您的应用程序需要这么长时间才能完成某些操作,请尝试对其进行分析。尝试针对您的程序运行Sampling Profiler。该帮助文件解释了如何将分析限制为仅应用程序的特定部分,您可以使用它来仅获取清理或创建部分的采样结果。这应该会向您展示您实际花费大部分时间的地方,并消除大量的猜测。

If you want to know why your app is taking so long to do something, try profiling it. Try running Sampling Profiler against your program. The helpfile explains how to limit the profiling to only a specific section of your app, which you could use to only get sampling results on the clearing or creating parts. This should show you where you're actually spending most of your time and take a lot of the guesswork out of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文