Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 创建并缓慢表示大量TLabel运行时_Performance_Delphi_Runtime_Delphi Xe2 - Fatal编程技术网

Performance 创建并缓慢表示大量TLabel运行时

Performance 创建并缓慢表示大量TLabel运行时,performance,delphi,runtime,delphi-xe2,Performance,Delphi,Runtime,Delphi Xe2,在我的应用程序中,我使用TLabelobjects运行时 有一个问题:表示20个标签需要很多时间(大约1-2秒) 父组件上的双缓冲区没有帮助Application.ProcessMessages只允许查看创建过程,而不允许查看frosen窗口 // creating a labels in loop aLabel:=TLabel.Create(scrlbx); labels.Add(aLabel); // TList for managing. with aLabel do begin Lef

在我的应用程序中,我使用
TLabel
objects运行时

有一个问题:表示20个标签需要很多时间(大约1-2秒)

父组件上的双缓冲区没有帮助
Application.ProcessMessages
只允许查看创建过程,而不允许查看frosen窗口

// creating a labels in loop 
aLabel:=TLabel.Create(scrlbx);
labels.Add(aLabel); // TList for managing.
with aLabel do begin
Left:=pointLeft;
    Top:=pointTop;
    Caption:='title';
    parent:=scrlbx; //TScrollBox
end;
pointTop:=pointTop+20;
将父循环外分配给另一个循环后会产生一些效果,但不能解决问题

for I := 0 to labels.Count-1 do begin
    TLabel(labels[i]).Parent:= scrlbx;
end;
禁用和启用
TScrollBox。可见的
before和aftel循环无效

附言: 物品的起绉不需要时间。 瓶颈是父级分配


upd:大量意味着大约500个项目。

如果您只是创建20个
TLabel
,添加
应用程序。Processmessages
不是一个好主意,因为它将重新绘制它以立即显示新添加的组件

对于常规应用程序,生成2000
TLabel
…只需不到1秒的时间。。。 如果我设置自定义样式,则需要更长的时间。。。也许,这是你的问题

因此,为了快速生成,我这样做:

uses ...,  System.StrUtils, System.Diagnostics, Vcl.Themes, Vcl.Styles;


procedure TForm3.GenerateLabels(bNoStyle: Boolean);
var
  iLoop, iMax: Integer;
  aLabel     : TLabel;
  pointLeft  : Integer;
  pointTop   : Integer;
  timeSpent  : TStopwatch;
begin
  iMax      := 2000;
  pointLeft :=  3;
  pointTop  :=  3;
  timeSpent := TStopwatch.StartNew;     // how long does it take
  if bNoStyle then
    TStyleManager.TrySetStyle('Windows'); // = no style
  for iLoop := 1 to iMax do
  begin
    Application.ProcessMessages;
    // creating a labels in loop
    aLabel:=TLabel.Create(Self);
    labels.Add(aLabel); // TList for managing.
    with aLabel do
    begin
      Left    := pointLeft;
      Top     := pointTop;
      Caption := 'title';
      parent  := scrlbx1; //TScrollBox
    end;
    pointTop  := pointTop + 20;
  end;
  if bNoStyle then
    TStyleManager.TrySetStyle('Carbon');  // previous style
  labels[0].Caption := IfThen(bNoStyle, 'No style', 'Style');
  labels[1].Caption := IntToStr(timeSpent.ElapsedMilliseconds) + 'ms';   // display the spent time
  labels[2].Caption := IntToStr(labels.Count);
  timeSpent.Stop;
end;

使用
滚动框。取消对齐
。启用登录

procedure TForm1.CreateLabels2;
var
  I: Integer;
  ALabel: TLabel;
  ATop: Integer;
begin
  ATop := 0;
  ScrollBox2.DisableAlign;
  for I := 0 to 2000 do
  begin
    ALabel := TLabel.Create(ScrollBox2);
    FLabels.Add(ALabel);
    with ALabel do
    begin
      Caption := 'Title';
      SetBounds(0, ATop, Width, Height);
      Parent := ScrollBox2;
    end;
    Inc(ATop, 20);
  end;
  ScrollBox2.EnableAlign;
end;

无法确认问题;测试和1000个标签创建在一个滚动框在眨眼。我还测试了2000。。。不到一秒钟。。。但是如果我激活一个自定义样式,所花费的时间就会爆炸……您是否有一个条件断点并编译为64位?我注意到,如果可能的话,这是需要避免的。我没有任何条件断点,编译为32。除了图标,我没有更改任何项目设置。是的,我对标签使用自定义样式。和启用/禁用对齐快速解决了一个问题。您的变体也解决了一个问题,但需要更多的时间来应用。原因是,虽然启用了align,但对于以前创建的变体,新的label调用realling方法,不是吗?@yujin1st Yes,只是说这是真的。但是没有Reallign方法,更没有完整的Reallign框架。