Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inthebrilliantblue multithreaded patch #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions WinThumbsPreloader/WinThumbsPreloader/AboutForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
<value>Bottom</value>
</data>
<data name="BorderBottom.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 287</value>
<value>0, 315</value>
</data>
<data name="BorderBottom.Size" type="System.Drawing.Size, System.Drawing">
<value>386, 1</value>
Expand All @@ -205,7 +205,7 @@
<value>Bottom</value>
</data>
<data name="BottomPanel.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 288</value>
<value>0, 316</value>
</data>
<data name="BottomPanel.Size" type="System.Drawing.Size, System.Drawing">
<value>386, 43</value>
Expand Down Expand Up @@ -361,25 +361,26 @@
<value>20, 20, 20, 20</value>
</data>
<data name="RichTextBox.Size" type="System.Drawing.Size, System.Drawing">
<value>356, 198</value>
<value>356, 226</value>
</data>
<data name="RichTextBox.TabIndex" type="System.Int32, mscorlib">
<value>3</value>
</data>
<data name="RichTextBox.Text" xml:space="preserve">
<value>To use this app start with a right click on a folder to call the context menu and select WinThumbsPreloader &gt; Preload thumbnails in the menu.

Also you can launch the app from command line:
Usage:
WinThumbsPreloader.exe [-r] &lt;path&gt;
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

<value>To use this app start with a right click on a folder to call the context menu and select WinThumbsPreloader &gt; Preload thumbnails in the menu.

Also you can launch the app from command line:
Usage:
WinThumbsPreloader.exe [-r] &lt;path&gt;
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</value>
</data>
<data name="&gt;&gt;RichTextBox.Name" xml:space="preserve">
Expand All @@ -404,7 +405,7 @@ Copyright (c) 2018 Dmitry Bruhov</value>
<value>15, 10, 15, 10</value>
</data>
<data name="ContentPanel.Size" type="System.Drawing.Size, System.Drawing">
<value>386, 218</value>
<value>386, 246</value>
</data>
<data name="ContentPanel.TabIndex" type="System.Int32, mscorlib">
<value>10</value>
Expand All @@ -428,7 +429,7 @@ Copyright (c) 2018 Dmitry Bruhov</value>
<value>6, 13</value>
</data>
<data name="$this.ClientSize" type="System.Drawing.Size, System.Drawing">
<value>386, 331</value>
<value>386, 359</value>
</data>
<data name="$this.StartPosition" type="System.Windows.Forms.FormStartPosition, System.Windows.Forms">
<value>CenterScreen</value>
Expand Down
15 changes: 14 additions & 1 deletion WinThumbsPreloader/WinThumbsPreloader/DirectoryScanner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -28,6 +28,19 @@ public IEnumerable<string> GetItems()
}
}

public List<string> GetItemsBulk() {
List<string> items = new List<string>();
if (includeNestedDirectories)
{
foreach (string item in GetItemsNested()) items.Add(item);
}
else
{
foreach (string item in GetItemsOnlyFirstLevel()) items.Add(item);
}
return items;
}

private IEnumerable<string> GetItemsOnlyFirstLevel()
{
string[] items = null;
Expand Down
34 changes: 26 additions & 8 deletions WinThumbsPreloader/WinThumbsPreloader/Options.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -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");
}
}
}
4 changes: 2 additions & 2 deletions WinThumbsPreloader/WinThumbsPreloader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Windows.Forms;

Expand Down Expand Up @@ -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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions WinThumbsPreloader/WinThumbsPreloader/ThumbnailPreloader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Expand Down Expand Up @@ -176,4 +176,4 @@ public enum WTS_ALPHATYPE : uint
WTSAT_ARGB = 2
}
}
}
}
40 changes: 31 additions & 9 deletions WinThumbsPreloader/WinThumbsPreloader/ThumbnailsPreloader.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -22,20 +23,23 @@ 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)
{
InitProgressDialog();
InitProgressDialogUpdateTimer();
}
_multiThreaded = multiThreaded;
Run();
}

Expand Down Expand Up @@ -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<string> 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();
}
Expand Down