Skip to content

Commit 19061df

Browse files
committed
Use concurrent version
1 parent e61151d commit 19061df

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using System.Globalization;
44
using System.IO;
5+
using System.Linq;
56
using System.Runtime.CompilerServices;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -157,22 +158,23 @@ public static string GetFileLastModifiedAt(string filePath, string previewPanelD
157158

158159
public static string GetFolderSize(string folderPath)
159160
{
161+
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
162+
160163
try
161164
{
165+
// Use parallel enumeration for better performance
162166
var directoryInfo = new DirectoryInfo(folderPath);
163-
long size = 0;
164-
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3));
165-
foreach (var file in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories))
166-
{
167-
if (cancellationTokenSource.Token.IsCancellationRequested)
168-
{
169-
// Timeout occurred, return unknown size
170-
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
171-
}
172-
size += file.Length;
173-
}
167+
long size = directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)
168+
.AsParallel()
169+
.WithCancellation(timeoutCts.Token)
170+
.Sum(file => file.Length);
171+
174172
return ResultManager.ToReadableSize(size, 2);
175173
}
174+
catch (OperationCanceledException)
175+
{
176+
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
177+
}
176178
catch (Exception e)
177179
{
178180
Main.Context.API.LogException(ClassName, $"Failed to get folder size for {folderPath}", e);

0 commit comments

Comments
 (0)