Skip to content

Commit e454ac5

Browse files
hishitetsuyaira2
andauthored
Code Quality: Fixed semaphore release timing in ItemViewModel (#14619)
Co-authored-by: Yair <39923744+yaira2@users.noreply.github.com>
1 parent 92126e3 commit e454ac5

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/Files.App/Data/Models/ItemViewModel.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ void ClearDisplay()
689689
// we have to call BeginBulkOperation to suppress CollectionChanged and call EndBulkOperation
690690
// in the end to fire a CollectionChanged event with NotifyCollectionChangedAction.Reset
691691
await bulkOperationSemaphore.WaitAsync(addFilesCTS.Token);
692+
var isSemaphoreReleased = false;
692693
try
693694
{
694695
FilesAndFolders.BeginBulkOperation();
@@ -710,11 +711,19 @@ void ApplyChanges()
710711

711712
void UpdateUI()
712713
{
713-
// Trigger CollectionChanged with NotifyCollectionChangedAction.Reset
714-
// once loading is completed so that UI can be updated
715-
FilesAndFolders.EndBulkOperation();
716-
UpdateEmptyTextType();
717-
DirectoryInfoUpdated?.Invoke(this, EventArgs.Empty);
714+
try
715+
{
716+
// Trigger CollectionChanged with NotifyCollectionChangedAction.Reset
717+
// once loading is completed so that UI can be updated
718+
FilesAndFolders.EndBulkOperation();
719+
UpdateEmptyTextType();
720+
DirectoryInfoUpdated?.Invoke(this, EventArgs.Empty);
721+
}
722+
finally
723+
{
724+
isSemaphoreReleased = true;
725+
bulkOperationSemaphore.Release();
726+
}
718727
}
719728

720729
if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && dispatcherQueue.HasThreadAccess)
@@ -726,11 +735,15 @@ void UpdateUI()
726735
{
727736
ApplyChanges();
728737
await dispatcherQueue.EnqueueOrInvokeAsync(UpdateUI);
738+
739+
// The semaphore will be released in UI thread
740+
isSemaphoreReleased = true;
729741
}
730742
}
731743
finally
732744
{
733-
bulkOperationSemaphore.Release();
745+
if (!isSemaphoreReleased)
746+
bulkOperationSemaphore.Release();
734747
}
735748
}
736749
catch (Exception ex)
@@ -830,6 +843,7 @@ public async Task GroupOptionsUpdatedAsync(CancellationToken token)
830843
try
831844
{
832845
await bulkOperationSemaphore.WaitAsync(token);
846+
var isSemaphoreReleased = false;
833847
try
834848
{
835849
FilesAndFolders.BeginBulkOperation();
@@ -854,12 +868,26 @@ await Task.Run(() =>
854868
if (token.IsCancellationRequested)
855869
return;
856870

857-
await dispatcherQueue.EnqueueOrInvokeAsync(
858-
FilesAndFolders.EndBulkOperation);
871+
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
872+
{
873+
try
874+
{
875+
FilesAndFolders.EndBulkOperation();
876+
}
877+
finally
878+
{
879+
isSemaphoreReleased = true;
880+
bulkOperationSemaphore.Release();
881+
}
882+
});
883+
884+
// The semaphore will be released in UI thread
885+
isSemaphoreReleased = true;
859886
}
860887
finally
861888
{
862-
bulkOperationSemaphore.Release();
889+
if (!isSemaphoreReleased)
890+
bulkOperationSemaphore.Release();
863891
}
864892
}
865893
catch (Exception ex)

0 commit comments

Comments
 (0)