From 19061df586b2d2d37b3757fb4ef25eb0d931cec3 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 5 Jun 2025 11:24:20 +0800 Subject: [PATCH] Use concurrent version --- .../Views/PreviewPanel.xaml.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs index ec60a3c13..5c250c8a8 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Globalization; using System.IO; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -157,22 +158,23 @@ public partial class PreviewPanel : UserControl, INotifyPropertyChanged public static string GetFolderSize(string folderPath) { + using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(3)); + try { + // Use parallel enumeration for better performance var directoryInfo = new DirectoryInfo(folderPath); - long size = 0; - using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)); - foreach (var file in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)) - { - if (cancellationTokenSource.Token.IsCancellationRequested) - { - // Timeout occurred, return unknown size - return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown"); - } - size += file.Length; - } + long size = directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories) + .AsParallel() + .WithCancellation(timeoutCts.Token) + .Sum(file => file.Length); + return ResultManager.ToReadableSize(size, 2); } + catch (OperationCanceledException) + { + return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown"); + } catch (Exception e) { Main.Context.API.LogException(ClassName, $"Failed to get folder size for {folderPath}", e);