Delphi 10.4 android app Listview with 3xxx items could cause wait for respond dialog in android 12 phone?

I have wrote an android app named “raiden memories backup” client/server with Delphi 10.4 to build home photo server.

My site is :
Google Play store: search “raiden memories backup”

The app works fine on android 10/11 until I got a new phone with android 12. I found the app will cause “no responding” issue when I get into path “DCIM/CAMERA” which has 3xxx files. It won’t happen at first time, but it surely happen after entering this folder several times, but it is not freezed actually and I can click [wait] button then continue the operation and it will popup again few seconds later (looping). I have no idea what happened. I think my code is quite simple.

Below is my code steps:

    folders := TDirectory.GetDirectories(path_tr);
    AddListItem(folders, 'folder');
    files := TDirectory.GetFiles(path_tr);
    AddListItem(files, 'file');

then part of AddListItem procedure is below:

    for c := 0 to length(list) - 1 do
    //xx  application.ProcessMessages;
      if stopadditem then
      Litem2 := Listview1.Items.Add;
      if itype = 'folder' then
        LItem2.ImageIndex := 2;
        LItem2.Text := ExtractFileName(list[c]);
        LItem2.Detail := list[c];
        LItem2.TagString := list[c];
        LItem2.Tag := 0;
                LItem2.ImageIndex := 3;
                LItem2.Text := ExtractFileName(list[c]);
                LItem2.Detail := FormatDateTime('yyyy-MM-dd hh:mm:ss', TFile.GetLastWriteTime(list[c]));
                LItem2.TagString := list[c];
                LItem2.Tag := 1;

I haven’t test it under Delphi 11.3 yet. Just wanna know is it an issue about TListview with large files in android12 ? May anyone give me some advices? Appreciate for any help. Thanks.

After dozens of retries, I found the key problem is occurred by

  button1.enabled := false;
  folders := TDirectory.GetDirectories(path_tr);
  files := TDirectory.GetFiles(path_tr);
  button1.enabled := true;

If I click button1 to run these two functions several times to get folders and files for a folder which contains larger number files, without adding item to listview, the loading will get slower everytime I click button1 then the app will popup “no responding” dialog repeatly eventually.
Thus, there is no issue for listview. The problem is TDirectory.

Anyone know how to avoid this kind of problem?

Can you put those calls in a TTask and TThread.Syncronize or TThread.Queue the results back to the main thread?

Thanks for your kindly reply.

At first, I only put listview related code into TThread.CreateAnonymousThread and TThread.Synchronize, and it won’t work.

Finally, I put the upper level codes including TDirectory functions into TThread.CreateAnonymousThread and TThread.Synchronize , it works like a charm as you suggested.

Thank you very much.