【问题标题】:Delphi FMX TListBox slow with large listsDelphi FMX TListBox 对于大型列表很慢
【发布时间】:2020-11-13 19:49:08
【问题描述】:

我正在将 2,000 个名称加载到 FMX TListBox 中,并且花费的时间太长(例如 35 秒或更长时间)。

这里是测试代码:

procedure TDocWindow.DEBUGAddLotsOfStringsToList;
var
  theTimeAtStart: Integer;
  J: Integer;

begin
  ListBox1.Clear;

  theTimeAtStart := TThread.GetTickCount;

  for J := 1 to 2200 do
    begin
      ListBox1.Items.Add(j.toString);
    end;

  ShowMessage('There were ' + J.ToString + ' strings added to the list in ' + (TThread.GetTickCount - theTimeAtStart).ToString + ' milliseconds.');
end;

TListBox 是否有什么东西让它对于几千个字符串来说太慢了?

【问题讨论】:

    标签: delphi firemonkey tlistbox delphi-10.4-sydney


    【解决方案1】:

    使用 BeginUpdate 和 EndUpdate 将我系统上的运行时间从 25 秒减少到大约 125 毫秒。

    procedure TForm1.Button1Click(Sender: TObject);
    var
      theTimeAtStart: Integer;
      J: Integer;
    begin
      theTimeAtStart := TThread.GetTickCount;
    
      ListBox1.Items.BeginUpdate;
    
      try
        ListBox1.Clear;
    
        for J := 1 to 2200 do
        begin
          ListBox1.Items.Add(j.ToString());
        end;
      finally
        ListBox1.Items.EndUpdate;
      end;
      ShowMessage('There were ' + J.ToString + ' strings added to the list in ' + (TThread.GetTickCount - theTimeAtStart).ToString + ' milliseconds.');
    end;
    

    【讨论】:

    • Clear() 也应该在 (Begin|End)Update 对内。
    • @RemyLebeau:已修复。谢谢。我在添加 Begin/EndUpdate 块时错过了这一点。
    • @KenWhite:你为什么把BeginUpdate放在try之后?
    • @AndreasRejbrand:出于同样的原因,我不小心在BeginUpdate 之前离开了Clear。 :-) 固定的。谢谢。
    猜你喜欢
    • 2020-05-31
    • 2018-09-06
    • 1970-01-01
    • 2021-01-07
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多