From b4e360c566bfc45b208cc33cf9038aae84fd5e33 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 3 Sep 2021 15:54:41 +0800 Subject: [PATCH] Minor fixes --- Files/Helpers/AsyncManualResetEvent.cs | 17 ++++++++++------- Files/ViewModels/ItemViewModel.cs | 6 ++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Files/Helpers/AsyncManualResetEvent.cs b/Files/Helpers/AsyncManualResetEvent.cs index 8f32f1cd39bd..9a69f892ab1c 100644 --- a/Files/Helpers/AsyncManualResetEvent.cs +++ b/Files/Helpers/AsyncManualResetEvent.cs @@ -11,13 +11,15 @@ public class AsyncManualResetEvent { private volatile TaskCompletionSource m_tcs = new TaskCompletionSource(); - public Task WaitAsync(CancellationToken cancellationToken = default) + public async Task WaitAsync(CancellationToken cancellationToken = default) { var tcs = m_tcs; + var cancelTcs = new TaskCompletionSource(); + cancellationToken.Register( - s => ((TaskCompletionSource)s).TrySetCanceled(), tcs); + s => ((TaskCompletionSource)s).TrySetCanceled(), cancelTcs); - return m_tcs.Task; + await await Task.WhenAny(tcs.Task, cancelTcs.Task); } private async Task Delay(int milliseconds) @@ -26,15 +28,15 @@ private async Task Delay(int milliseconds) return false; } - public Task WaitAsync(int milliseconds, CancellationToken cancellationToken = default) + public async Task WaitAsync(int milliseconds, CancellationToken cancellationToken = default) { var tcs = m_tcs; + var cancelTcs = new TaskCompletionSource(); cancellationToken.Register( - s => ((TaskCompletionSource)s).TrySetCanceled(), tcs); - + s => ((TaskCompletionSource)s).TrySetCanceled(), cancelTcs); - return Task.WhenAny(m_tcs.Task, Delay(milliseconds)).Result; + return await await Task.WhenAny(tcs.Task, cancelTcs.Task, Delay(milliseconds)); } public void Set() @@ -43,6 +45,7 @@ public void Set() Task.Factory.StartNew(s => ((TaskCompletionSource)s).TrySetResult(true), tcs, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default); tcs.Task.Wait(); + } public void Reset() diff --git a/Files/ViewModels/ItemViewModel.cs b/Files/ViewModels/ItemViewModel.cs index 9d16736c4cbe..86921cb86e69 100644 --- a/Files/ViewModels/ItemViewModel.cs +++ b/Files/ViewModels/ItemViewModel.cs @@ -909,11 +909,13 @@ public async Task LoadExtendedItemProperties(ListedItem item, uint thumbnailSize item.ItemPropertiesInitialized = true; itemLoadQueue[item.ItemPath] = false; + var cts = loadPropsCTS; + try { await Task.Run(async () => { - await loadPropsEvent.WaitAsync(loadPropsCTS.Token); + await loadPropsEvent.WaitAsync(cts.Token); if (itemLoadQueue.TryGetValue(item.ItemPath, out var canceled) && canceled) { @@ -1039,7 +1041,7 @@ await FilesystemTasks.Wrap(() => CoreApplication.MainView.DispatcherQueue.Enqueu })); } } - }, loadPropsCTS.Token); + }, cts.Token); } catch (OperationCanceledException) {