Skip to content

Commit 8e138cd

Browse files
authored
Fix: Fixed InvalidOperationException in ShellViewModel.FolderSizeProvider_SizeChanged (#16242)
1 parent da09750 commit 8e138cd

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/Files.App/Services/SizeProvider/CachedSizeProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public async Task UpdateAsync(string path, CancellationToken cancellationToken)
3737
RaiseSizeChanged(path, 0, SizeChangedValueState.None);
3838
}
3939

40+
var stopwatch = Stopwatch.StartNew();
4041
ulong size = await Calculate(path);
4142

4243
sizes[path] = size;
@@ -81,8 +82,10 @@ async Task<ulong> Calculate(string path, int level = 0)
8182
await Task.Yield();
8283
sizes[localPath] = localSize;
8384
}
84-
if (level is 0)
85+
if (level is 0 && stopwatch.ElapsedMilliseconds > 500)
8586
{
87+
// Limit updates to every 0.5 seconds to prevent crashes due to frequent updates
88+
stopwatch.Restart();
8689
RaiseSizeChanged(path, size, SizeChangedValueState.Intermediate);
8790
}
8891

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ private async void FolderSizeProvider_SizeChanged(object? sender, Services.SizeP
603603
try
604604
{
605605
var matchingItem = filesAndFolders.ToList().FirstOrDefault(x => x.ItemPath == e.Path);
606-
if (matchingItem is not null)
606+
if (matchingItem is not null && (e.ValueState is not SizeChangedValueState.Intermediate || (long)e.NewSize > matchingItem.FileSizeBytes))
607607
{
608608
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
609609
{

0 commit comments

Comments
 (0)