Add timeout for folder size calculation

This commit is contained in:
Jack251970 2025-06-04 17:36:37 +08:00
parent b1557dd4af
commit edb4d743e9

View file

@ -3,6 +3,7 @@ using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -149,8 +150,15 @@ public partial class PreviewPanel : UserControl, INotifyPropertyChanged
{
var directoryInfo = new DirectoryInfo(folderPath);
long size = 0;
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3));
foreach (var file in directoryInfo.GetFiles("*", SearchOption.AllDirectories))
{
if (cancellationTokenSource.Token.IsCancellationRequested)
{
// Timeout occurred, return unknown size
cancellationTokenSource.Dispose();
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
}
size += file.Length;
}
return ResultManager.ToReadableSize(size, 2);