Use concurrent version

This commit is contained in:
Jack251970 2025-06-05 11:24:20 +08:00
parent e61151dc43
commit 19061df586

View file

@ -2,6 +2,7 @@
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -157,22 +158,23 @@ public partial class PreviewPanel : UserControl, INotifyPropertyChanged
public static string GetFolderSize(string folderPath) public static string GetFolderSize(string folderPath)
{ {
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
try try
{ {
// Use parallel enumeration for better performance
var directoryInfo = new DirectoryInfo(folderPath); var directoryInfo = new DirectoryInfo(folderPath);
long size = 0; long size = directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)); .AsParallel()
foreach (var file in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)) .WithCancellation(timeoutCts.Token)
{ .Sum(file => file.Length);
if (cancellationTokenSource.Token.IsCancellationRequested)
{
// Timeout occurred, return unknown size
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
}
size += file.Length;
}
return ResultManager.ToReadableSize(size, 2); return ResultManager.ToReadableSize(size, 2);
} }
catch (OperationCanceledException)
{
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
}
catch (Exception e) catch (Exception e)
{ {
Main.Context.API.LogException(ClassName, $"Failed to get folder size for {folderPath}", e); Main.Context.API.LogException(ClassName, $"Failed to get folder size for {folderPath}", e);