Multithreading AddFoldersAndFilesToTree

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Multithreading AddFoldersAndFilesToTree

24 May 2017, 05:00

I do wonder if multithreading add folders and files (huge amounts of folders and files) would accomplish faster treeview loadtime?

Code: Select all

AddFoldersAndFilesToTree(Folder, ParentID := 0, First := 0) {
   ;FileFilter := FileFilters[IniSec] ;*FILEFILTER*
;--FOLDER HANDLING------------------------------------------------------------------------------------------------------------------
   SplitPath, Folder, FolderName
   If (ParentID = 0)
      FolderID := TV_Add(Folder, ParentID, "Icon2 Expand") ; <<<<<Step3.9.1
   Else If (First = 0)
      FolderID := TV_Add(FolderName, ParentID, "Icon2" . (FileFilter ? " Expand" : ""))  ; <<<<<Step3.9.1
   Else If (First = 1)
      FolderID := TV_Add(FolderName, ParentID, "First Icon2" . (FileFilter ? " Expand" : ""))  ; <<<<<Step3.9.1
   TVCache[FolderID] := {Path: Folder, IsDir: 1}
   TVCache[Folder] := {ID: FolderID, IsDir: 1}
   ; -------------------------------------------------------------------------------------------------------------------------------
   If (Settings.Recurse = "1")
   {
      FolderList := ""
      Loop, Files, % Folder "\*.*", D ; <<<<< removed option R
      {
         FolderList .= (A_Index > 1 ? "`n" : "") . A_LoopFileTimeModified . "|" . A_LoopFileLongPath
      }
      If (FolderList <> "") {                      ; if subfolders were found
         If (Settings.SortRev = "1")               ; Sort folders reverse enabled
            Sort, FolderList, F ReverseDirection   ; Reverses the list so that it contains 4,3,2,1
         If (Settings.SortTimeRev = "1")           ; Sort folders timereverse enabled
            Sort, FolderList, F SortFunc           ; Sort folders TimeModified, newest on top to oldest in bottom
         Loop, Parse, FolderList, `n
            AddFoldersAndFilesToTree(SubStr(A_LoopField, 16), FolderID) ; <<<<<20151203
      }
   }
;--FILE HANDLING--------------------------------------------------------------------------------------------------------------------
   ; Create the list of files
   FileList := ""
   Loop, % Folder . "\*.*", 0
   {
      If ((Settings.AllFiles = "1") || (Settings[A_LoopFileExt . "Files"] = "1"))
      ;&& ((FileFilter = "") || RegExMatch(A_LoopFileLongPath, "i)" . FileFilter)) ; <<<<< can the FileFilter be used here ???
         FileList .= (FileList <> "" ? "`n" : "") . A_LoopFileTimeCreated . "|" . A_LoopFileLongPath . "|" . A_LoopFileTimeModified ;. "|" . A_LoopFileSize
   }
   ; -------------------------------------------------------------------------------------------------------------------------------
   If (FileFilter)
   {
      SortList := ""
	  
      Loop, Parse, FileList, `n, `r
      {
       ;If RegExMatch(A_LoopField, "i)(.*?)(\Q" FileFilter "\E.*)", Half)  ; \Q & \E are recommended In case "filter" includes special regex characters
       ;If RegExMatch(A_LoopField, "i)(.+?)(" . FileFilter . ".+)", Half)   ; two (subpatterns)
	   ;If RegExMatch(A_LoopField, "i)(.+\\\K.+)(" . FileFilter . ".+)", Half)   ; two (subpatterns) excludes if FileFilter exists in file path
	   If RegExMatch(A_LoopField, "i)(.+\\\K.+?)(" . FileFilter . ".+)", Half)   ; two (subpatterns) excludes if FileFilter exists in file path (non-greedy)
         SortList .= (SortList <> "" ? "`n" : "") . Half2 . A_Tab . Half1 ; Places Half1 after Half2 separated by a tab.
      }
	  If (Settings.SortRev = "1") ; Sort files reverse enabled
       Sort, SortList, R
      Else
       Sort, SortList, CL
	  FileList := ""
      Loop, Parse, SortList, `n
      {
         StringSplit, Half, A_LoopField, %A_Tab% ; List is now sorted and Half2 has now the same value as Half1 had In previous loop.
         FileList .= (FileList <> "" ? "`n" : "") . Half2 . Half1 ; Joining together so file gets the same name as before.
      }
   }
   ; -------------------------------------------------------------------------------------------------------------------------------
   ; Sort (if required) and add the files
   Else {
      If (FileList <> "") {
       If (Settings.SortRev = "1")           ; Sort files reverse enabled
         Sort, FileList, F ReverseDirection  ; Reverses the list so that it contains 4,3,2,1
       If (Settings.SortTimeRev = "1")       ; Sort files timereverse enabled
         Sort, FileList, F SortFunc          ; Sort folders TimeModified, newest on top to oldest in bottom
      }
   }
   Loop, Parse, FileList, `n
   {
	  Split := StrSplit(A_LoopField, "|")
      SplitPath, % Split.2, Name, , Ext
      If (Split.1 = Split.3) { ; recording isn't finished yet
         If MovieExt[Ext]
            FileID := TV_Add(Name, FolderID, "Icon3")  ; <<<<<Step3.9.1
         Else
            FileID := TV_Add(Name, FolderID, "Icon" . ((Index := TVIcons[Ext]) ? Index : 1)) ; <<<<<Step3.9.1
      }
      Else                   ; recording is finished
         FileID := TV_Add(Name, FolderID, "Icon" . ((Index := TVIcons[Ext]) ? Index : 1)) ; <<<<<Step3.9.1
      TVCache[FileID] := {Path: Split.2, IsDir: 0}
	  ;TVCache[FileID] := {Path: Split.2, IsDir: 0, Size: Split.4} ; +++++ add Size in array
      TVCache[Split.2] := {ID: FileID, IsDir: 0}
	  ;FileAppend, %Name%`n, F:\TV\test\MediaLauncher\FilesList.txt ; <<<<< needed only for testing ???
   }
   ; -------------------------------------------------------------------------------------------------------------------------------
   ; Remove empty folder nodes if a file filter is set
   If (FileFilter) && (ParentID <> 0) && !TV_GetChild(FolderID) {
      TV_Delete(FolderID)
      TVCache.Delete(TVCache[FolderID, "Path"])
      TVCache.Delete(FolderID)
   }
   ; Else
     ; FileAppend, %Folder%`n, F:\TV\test\MediaLauncher\FolderList.txt ; <<<<< needed only for testing ???
   ; -------------------------------------------------------------------------------------------------------------------------------
   Return TV_GetCount()
}
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: Multithreading AddFoldersAndFilesToTree

24 May 2017, 06:04

Stackoverflow advise strongly against multithreading writing to UI, but reading (the fileLists) is no problem. Hmm..ok maybe this is not the right way to go. Load only what people can see in a first stage and then a second stage background loading might be the better way.

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 4 guests