diff --git a/WinThumbsPreloader/WinThumbsPreloader/AboutForm.resx b/WinThumbsPreloader/WinThumbsPreloader/AboutForm.resx index 0b6da0c..9af6f86 100644 --- a/WinThumbsPreloader/WinThumbsPreloader/AboutForm.resx +++ b/WinThumbsPreloader/WinThumbsPreloader/AboutForm.resx @@ -181,7 +181,7 @@ Bottom - 0, 287 + 0, 315 386, 1 @@ -205,7 +205,7 @@ Bottom - 0, 288 + 0, 316 386, 43 @@ -361,25 +361,26 @@ 20, 20, 20, 20 - 356, 198 + 356, 226 3 - To use this app start with a right click on a folder to call the context menu and select WinThumbsPreloader > Preload thumbnails in the menu. - -Also you can launch the app from command line: -Usage: - WinThumbsPreloader.exe [-r] <path> -Options: - -r Recursively preload thumbnails in nested folders. - -s Silent mode - don't show a progress dialog. - - -Website: https://bruhov.com/WinThumbsPreloader -Project page: https://github.com/bruhov/WinThumbsPreloader - + To use this app start with a right click on a folder to call the context menu and select WinThumbsPreloader > Preload thumbnails in the menu. + +Also you can launch the app from command line: +Usage: + WinThumbsPreloader.exe [-r] <path> +Options: + -r Recursively preload thumbnails in nested folders. + -s Silent mode - don't show a progress dialog. + -m Multi-Threaded mode + + +Website: https://bruhov.com/WinThumbsPreloader +Project page: https://github.com/bruhov/WinThumbsPreloader + Copyright (c) 2018 Dmitry Bruhov @@ -404,7 +405,7 @@ Copyright (c) 2018 Dmitry Bruhov 15, 10, 15, 10 - 386, 218 + 386, 246 10 @@ -428,7 +429,7 @@ Copyright (c) 2018 Dmitry Bruhov 6, 13 - 386, 331 + 386, 359 CenterScreen diff --git a/WinThumbsPreloader/WinThumbsPreloader/DirectoryScanner.cs b/WinThumbsPreloader/WinThumbsPreloader/DirectoryScanner.cs index b8f62a6..df2e600 100644 --- a/WinThumbsPreloader/WinThumbsPreloader/DirectoryScanner.cs +++ b/WinThumbsPreloader/WinThumbsPreloader/DirectoryScanner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -28,6 +28,19 @@ public IEnumerable GetItems() } } + public List GetItemsBulk() { + List items = new List(); + if (includeNestedDirectories) + { + foreach (string item in GetItemsNested()) items.Add(item); + } + else + { + foreach (string item in GetItemsOnlyFirstLevel()) items.Add(item); + } + return items; + } + private IEnumerable GetItemsOnlyFirstLevel() { string[] items = null; diff --git a/WinThumbsPreloader/WinThumbsPreloader/Options.cs b/WinThumbsPreloader/WinThumbsPreloader/Options.cs index cf28bde..3a5234b 100644 --- a/WinThumbsPreloader/WinThumbsPreloader/Options.cs +++ b/WinThumbsPreloader/WinThumbsPreloader/Options.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -13,21 +13,39 @@ class Options public bool includeNestedDirectories; public bool silentMode; public string path; + public bool multithreaded; public Options(string[] arguments) { - badArguments = (arguments.Length == 0 || arguments.Length > 2); + //Check if we have more arguments than we support + badArguments = (arguments.Length == 0 || arguments.Length > 4); if (badArguments) return; - bool optionsProvided = (arguments.Length == 2); - string rawOptions = (optionsProvided ? arguments[0] : ""); - path = arguments[optionsProvided ? 1 : 0]; + //Set default options + includeNestedDirectories = false; + silentMode = false; + multithreaded = false; + //Set the options the user wants from the arguments + foreach (string argu in arguments) { + switch (argu) { + case "-r": + includeNestedDirectories = true; + break; + case "-s": + silentMode = true; + break; + case "-m": + multithreaded = true; + break; + default: + path = argu; + break; + } + } + //Check if the path we grabbed is real badArguments = !Directory.Exists(path); if (badArguments) return; - - includeNestedDirectories = rawOptions.Contains("r"); - silentMode = rawOptions.Contains("s"); } } } diff --git a/WinThumbsPreloader/WinThumbsPreloader/Program.cs b/WinThumbsPreloader/WinThumbsPreloader/Program.cs index 3f8209a..65e65f8 100644 --- a/WinThumbsPreloader/WinThumbsPreloader/Program.cs +++ b/WinThumbsPreloader/WinThumbsPreloader/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Windows.Forms; @@ -29,7 +29,7 @@ static void Main(string[] arguments) } else { - new ThumbnailsPreloader(options.path, options.includeNestedDirectories, options.silentMode); + new ThumbnailsPreloader(options.path, options.includeNestedDirectories, options.silentMode, options.multithreaded); Application.Run(); } } diff --git a/WinThumbsPreloader/WinThumbsPreloader/ThumbnailPreloader.cs b/WinThumbsPreloader/WinThumbsPreloader/ThumbnailPreloader.cs index 8c2477b..11a8a05 100644 --- a/WinThumbsPreloader/WinThumbsPreloader/ThumbnailPreloader.cs +++ b/WinThumbsPreloader/WinThumbsPreloader/ThumbnailPreloader.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.InteropServices; using System.Windows.Forms; @@ -176,4 +176,4 @@ public enum WTS_ALPHATYPE : uint WTSAT_ARGB = 2 } } -} \ No newline at end of file +} diff --git a/WinThumbsPreloader/WinThumbsPreloader/ThumbnailsPreloader.cs b/WinThumbsPreloader/WinThumbsPreloader/ThumbnailsPreloader.cs index 5ff8cd4..1a235ae 100644 --- a/WinThumbsPreloader/WinThumbsPreloader/ThumbnailsPreloader.cs +++ b/WinThumbsPreloader/WinThumbsPreloader/ThumbnailsPreloader.cs @@ -1,8 +1,9 @@ -using System; -using System.Threading.Tasks; +using System; using System.Windows.Forms; using System.IO; using WinThumbsPreloader.Properties; +using System.Threading.Tasks; +using System.Collections.Generic; namespace WinThumbsPreloader { @@ -22,13 +23,15 @@ class ThumbnailsPreloader private ProgressDialog progressDialog; private Timer progressDialogUpdateTimer; + protected bool _multiThreaded; + public ThumbnailsPreloaderState state = ThumbnailsPreloaderState.GettingNumberOfItems; public ThumbnailsPreloaderState prevState = ThumbnailsPreloaderState.New; public int totalItemsCount = 0; public int processedItemsCount = 0; public string currentFile = ""; - public ThumbnailsPreloader(string path, bool includeNestedDirectories, bool silentMode) + public ThumbnailsPreloader(string path, bool includeNestedDirectories, bool silentMode, bool multiThreaded) { directoryScanner = new DirectoryScanner(path, includeNestedDirectories); if (!silentMode) @@ -36,6 +39,7 @@ public ThumbnailsPreloader(string path, bool includeNestedDirectories, bool sile InitProgressDialog(); InitProgressDialogUpdateTimer(); } + _multiThreaded = multiThreaded; Run(); } @@ -112,14 +116,32 @@ await Task.Run(() => //Start processing state = ThumbnailsPreloaderState.Processing; ThumbnailPreloader thumbnailPreloader = new ThumbnailPreloader(); - foreach (string item in directoryScanner.GetItems()) + //Get the items first before doing work + List items = directoryScanner.GetItemsBulk(); + if (!_multiThreaded) { - currentFile = item; - thumbnailPreloader.PreloadThumbnail(item); - processedItemsCount++; - if (processedItemsCount == totalItemsCount) state = ThumbnailsPreloaderState.Done; - if (state == ThumbnailsPreloaderState.Canceled) return; + foreach (string item in items) + { + currentFile = item; + thumbnailPreloader.PreloadThumbnail(item); + processedItemsCount++; + if (processedItemsCount == totalItemsCount) state = ThumbnailsPreloaderState.Done; + if (state == ThumbnailsPreloaderState.Canceled) return; + } } + else { + Parallel.ForEach( + items, + new ParallelOptions { MaxDegreeOfParallelism = 2048 }, + item => + { + currentFile = item; + thumbnailPreloader.PreloadThumbnail(item); + processedItemsCount++; + if (processedItemsCount == totalItemsCount) state = ThumbnailsPreloaderState.Done; + if (state == ThumbnailsPreloaderState.Canceled) return; + }); + } }); Application.Exit(); }