@@ -689,6 +689,7 @@ void ClearDisplay()
689
689
// we have to call BeginBulkOperation to suppress CollectionChanged and call EndBulkOperation
690
690
// in the end to fire a CollectionChanged event with NotifyCollectionChangedAction.Reset
691
691
await bulkOperationSemaphore . WaitAsync ( addFilesCTS . Token ) ;
692
+ var isSemaphoreReleased = false ;
692
693
try
693
694
{
694
695
FilesAndFolders . BeginBulkOperation ( ) ;
@@ -710,11 +711,19 @@ void ApplyChanges()
710
711
711
712
void UpdateUI ( )
712
713
{
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
+ }
718
727
}
719
728
720
729
if ( NativeWinApiHelper . IsHasThreadAccessPropertyPresent && dispatcherQueue . HasThreadAccess )
@@ -726,11 +735,15 @@ void UpdateUI()
726
735
{
727
736
ApplyChanges ( ) ;
728
737
await dispatcherQueue . EnqueueOrInvokeAsync ( UpdateUI ) ;
738
+
739
+ // The semaphore will be released in UI thread
740
+ isSemaphoreReleased = true ;
729
741
}
730
742
}
731
743
finally
732
744
{
733
- bulkOperationSemaphore . Release ( ) ;
745
+ if ( ! isSemaphoreReleased )
746
+ bulkOperationSemaphore . Release ( ) ;
734
747
}
735
748
}
736
749
catch ( Exception ex )
@@ -830,6 +843,7 @@ public async Task GroupOptionsUpdatedAsync(CancellationToken token)
830
843
try
831
844
{
832
845
await bulkOperationSemaphore . WaitAsync ( token ) ;
846
+ var isSemaphoreReleased = false ;
833
847
try
834
848
{
835
849
FilesAndFolders . BeginBulkOperation ( ) ;
@@ -854,12 +868,26 @@ await Task.Run(() =>
854
868
if ( token . IsCancellationRequested )
855
869
return ;
856
870
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 ;
859
886
}
860
887
finally
861
888
{
862
- bulkOperationSemaphore . Release ( ) ;
889
+ if ( ! isSemaphoreReleased )
890
+ bulkOperationSemaphore . Release ( ) ;
863
891
}
864
892
}
865
893
catch ( Exception ex )
0 commit comments