Skip to content

Code Quality: Fixed semaphore release timing in ItemViewModel #14619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ void ClearDisplay()
// we have to call BeginBulkOperation to suppress CollectionChanged and call EndBulkOperation
// in the end to fire a CollectionChanged event with NotifyCollectionChangedAction.Reset
await bulkOperationSemaphore.WaitAsync(addFilesCTS.Token);
var isSemaphoreReleased = false;
try
{
FilesAndFolders.BeginBulkOperation();
Expand All @@ -710,11 +711,19 @@ void ApplyChanges()

void UpdateUI()
{
// Trigger CollectionChanged with NotifyCollectionChangedAction.Reset
// once loading is completed so that UI can be updated
FilesAndFolders.EndBulkOperation();
UpdateEmptyTextType();
DirectoryInfoUpdated?.Invoke(this, EventArgs.Empty);
try
{
// Trigger CollectionChanged with NotifyCollectionChangedAction.Reset
// once loading is completed so that UI can be updated
FilesAndFolders.EndBulkOperation();
UpdateEmptyTextType();
DirectoryInfoUpdated?.Invoke(this, EventArgs.Empty);
}
finally
{
isSemaphoreReleased = true;
bulkOperationSemaphore.Release();
}
}

if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && dispatcherQueue.HasThreadAccess)
Expand All @@ -726,11 +735,15 @@ void UpdateUI()
{
ApplyChanges();
await dispatcherQueue.EnqueueOrInvokeAsync(UpdateUI);

// The semaphore will be released in UI thread
isSemaphoreReleased = true;
}
}
finally
{
bulkOperationSemaphore.Release();
if (!isSemaphoreReleased)
bulkOperationSemaphore.Release();
}
}
catch (Exception ex)
Expand Down Expand Up @@ -830,6 +843,7 @@ public async Task GroupOptionsUpdatedAsync(CancellationToken token)
try
{
await bulkOperationSemaphore.WaitAsync(token);
var isSemaphoreReleased = false;
try
{
FilesAndFolders.BeginBulkOperation();
Expand All @@ -854,12 +868,26 @@ await Task.Run(() =>
if (token.IsCancellationRequested)
return;

await dispatcherQueue.EnqueueOrInvokeAsync(
FilesAndFolders.EndBulkOperation);
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
try
{
FilesAndFolders.EndBulkOperation();
}
finally
{
isSemaphoreReleased = true;
bulkOperationSemaphore.Release();
}
});

// The semaphore will be released in UI thread
isSemaphoreReleased = true;
}
finally
{
bulkOperationSemaphore.Release();
if (!isSemaphoreReleased)
bulkOperationSemaphore.Release();
}
}
catch (Exception ex)
Expand Down