From 467b2160ad616aa3393bb29e176780e5d98e44a1 Mon Sep 17 00:00:00 2001 From: Steven He Date: Mon, 15 Feb 2021 12:50:21 +0800 Subject: [PATCH 1/2] Let GC collect CancellationTokenSource to prevent System.ObjectDisposedException --- Files/Files.csproj | 1 - Files/ViewModels/ItemViewModel.cs | 7 ------- Files/Views/Pages/Properties.xaml.cs | 2 -- 3 files changed, 10 deletions(-) diff --git a/Files/Files.csproj b/Files/Files.csproj index 8993f2d3e6e7..031b1c4ac9f3 100644 --- a/Files/Files.csproj +++ b/Files/Files.csproj @@ -170,7 +170,6 @@ - DynamicDialog.xaml diff --git a/Files/ViewModels/ItemViewModel.cs b/Files/ViewModels/ItemViewModel.cs index 5c1e624ceb87..006638e37e06 100644 --- a/Files/ViewModels/ItemViewModel.cs +++ b/Files/ViewModels/ItemViewModel.cs @@ -476,7 +476,6 @@ public void CancelLoadAndClearFiles() public void CancelExtendedPropertiesLoading() { loadPropsCTS.Cancel(); - loadPropsCTS.Dispose(); loadPropsCTS = new CancellationTokenSource(); } @@ -921,7 +920,6 @@ public async void RapidAddItemsToCollectionAsync(string path, string previousDir { // Drop all the other waiting instances semaphoreCTS.Cancel(); - semaphoreCTS.Dispose(); semaphoreCTS = new CancellationTokenSource(); IsLoadingItems = true; @@ -1028,7 +1026,6 @@ await folders.AsyncParallelForEach(async (folder) => if (addFilesCTS.IsCancellationRequested) { - addFilesCTS.Dispose(); addFilesCTS = new CancellationTokenSource(); IsLoadingItems = false; return; @@ -1605,7 +1602,6 @@ private void WatchForDirectoryChanges(string path) CloseHandle(overlapped.hEvent); operationQueue.Clear(); cts.Cancel(); - cts.Dispose(); Debug.WriteLine("aWatcherAction done: {0}", rand); }); @@ -1924,9 +1920,6 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") public void Dispose() { CancelLoadAndClearFiles(); - addFilesCTS?.Dispose(); - semaphoreCTS?.Dispose(); - loadPropsCTS?.Dispose(); } } diff --git a/Files/Views/Pages/Properties.xaml.cs b/Files/Views/Pages/Properties.xaml.cs index a1b641cbd6fa..436abd0f585f 100644 --- a/Files/Views/Pages/Properties.xaml.cs +++ b/Files/Views/Pages/Properties.xaml.cs @@ -128,7 +128,6 @@ private void Properties_Consolidated(ApplicationView sender, ApplicationViewCons if (tokenSource != null && !tokenSource.IsCancellationRequested) { tokenSource.Cancel(); - tokenSource.Dispose(); tokenSource = null; } } @@ -141,7 +140,6 @@ private void PropertiesDialog_Closed(ContentDialog sender, ContentDialogClosedEv if (tokenSource != null && !tokenSource.IsCancellationRequested) { tokenSource.Cancel(); - tokenSource.Dispose(); tokenSource = null; } propertiesDialog.Hide(); From befa355b1bc83413c6a9acce815dfc2fd7814897 Mon Sep 17 00:00:00 2001 From: Steven He Date: Wed, 17 Feb 2021 12:19:47 +0800 Subject: [PATCH 2/2] Remove redundant ObjectDisposedException catches --- Files/ViewModels/ItemViewModel.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Files/ViewModels/ItemViewModel.cs b/Files/ViewModels/ItemViewModel.cs index 006638e37e06..449f1ac58d67 100644 --- a/Files/ViewModels/ItemViewModel.cs +++ b/Files/ViewModels/ItemViewModel.cs @@ -752,7 +752,7 @@ await Task.Run(async () => { await loadExtendedPropsSemaphore.WaitAsync(loadPropsCTS.Token); } - catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException) + catch (OperationCanceledException) { return; } @@ -911,7 +911,7 @@ public async void RapidAddItemsToCollectionAsync(string path, string previousDir // simply drop this instance await enumFolderSemaphore.WaitAsync(semaphoreCTS.Token); } - catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException) + catch (OperationCanceledException) { return; } @@ -1077,10 +1077,6 @@ await folders.AsyncParallelForEach(async (folder) => } } } - catch (ObjectDisposedException ex) - { - NLog.LogManager.GetCurrentClassLogger().Warn(ex, ex.Message); - } finally { enumFolderSemaphore.Release(); @@ -1831,7 +1827,7 @@ private async Task UpdateFileOrFolderAsync(string path) { await enumFolderSemaphore.WaitAsync(semaphoreCTS.Token); } - catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException) + catch (OperationCanceledException) { return; } @@ -1876,7 +1872,7 @@ public async Task RemoveFileOrFolderAsync(string path) { await enumFolderSemaphore.WaitAsync(semaphoreCTS.Token); } - catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException) + catch (OperationCanceledException) { return; }