From 92407a0fc3c15920536011c8c1831b8fec782bdb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 3 Dec 2020 20:18:36 +0100 Subject: [PATCH 1/2] Removed leftover DispatcherHelper usages --- .../DispatcherQueueHelperPage.xaml.cs | 3 +-- .../Shell.SamplePicker.cs | 3 ++- Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs | 3 ++- Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs | 3 ++- .../Extensions/AnimationExtensions.Light.cs | 6 +++--- ...ScrollViewerExtensions.MiddleClickScrolling.cs | 15 ++------------- .../XamlIslandsTest_Gaze.cs | 7 ++++--- 7 files changed, 16 insertions(+), 24 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs index fe61f6ea479..bde2d707c1f 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs @@ -3,10 +3,9 @@ // See the LICENSE file in the project root for more information. using System.Threading.Tasks; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.System; using Windows.UI.Xaml; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages { diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Shell.SamplePicker.cs b/Microsoft.Toolkit.Uwp.SampleApp/Shell.SamplePicker.cs index 80cc95787c6..b52099921b0 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Shell.SamplePicker.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Shell.SamplePicker.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Numerics; using System.Threading.Tasks; +using Microsoft.Toolkit.Uwp.Extensions; using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.SampleApp.Pages; using Microsoft.Toolkit.Uwp.UI.Animations; @@ -147,7 +148,7 @@ private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sende ShowSamplePicker(category.Samples, true); // Then Focus on Picker - DispatcherHelper.ExecuteOnUIThreadAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard)); + dispatcherQueue.EnqueueAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard)); } } else if (args.IsSettingsInvoked) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs b/Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs index 34730cc0a74..0dd641d3f66 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Linq; +using Microsoft.Toolkit.Uwp.Extensions; using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.UI.Extensions; using Windows.UI.Xaml; @@ -76,7 +77,7 @@ private void SearchBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEve if (e.Key == Windows.System.VirtualKey.Down && SamplePickerGrid.Visibility == Windows.UI.Xaml.Visibility.Visible) { // If we try and navigate down out of the textbox (and there's search results), go to the search results. - DispatcherHelper.ExecuteOnUIThreadAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard)); + dispatcherQueue.EnqueueAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard)); } } diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs index d2f9ca0754b..44f63d1a251 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs @@ -7,7 +7,6 @@ using Microsoft.Toolkit.Uwp.SampleApp.Pages; using Microsoft.Toolkit.Uwp.UI.Extensions; using Windows.System; -using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; @@ -16,6 +15,8 @@ namespace Microsoft.Toolkit.Uwp.SampleApp { public sealed partial class Shell { + private readonly DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + public static Shell Current { get; private set; } public Shell() diff --git a/Microsoft.Toolkit.Uwp.UI.Animations/Extensions/AnimationExtensions.Light.cs b/Microsoft.Toolkit.Uwp.UI.Animations/Extensions/AnimationExtensions.Light.cs index 88573df78c9..c897d4cb206 100644 --- a/Microsoft.Toolkit.Uwp.UI.Animations/Extensions/AnimationExtensions.Light.cs +++ b/Microsoft.Toolkit.Uwp.UI.Animations/Extensions/AnimationExtensions.Light.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; using Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas.Effects; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.UI; using Windows.UI.Composition; using Windows.UI.Composition.Effects; @@ -102,7 +102,7 @@ public static AnimationSet Light( var task = new AnimationTask(); task.AnimationSet = animationSet; - task.Task = DispatcherHelper.ExecuteOnUIThreadAsync( + task.Task = visual.DispatcherQueue.EnqueueAsync( () => { const string sceneName = "PointLightScene"; @@ -184,7 +184,7 @@ public static AnimationSet Light( } pointLights[visual] = pointLight; - }, Windows.UI.Core.CoreDispatcherPriority.Normal); + }); animationSet.AddAnimationThroughTask(task); return animationSet; diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs index 28bd87d9841..d603592c3a8 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs @@ -162,10 +162,7 @@ private static void Scroll(object state) offsetX = offsetX > _maxSpeed ? _maxSpeed : offsetX; offsetY = offsetY > _maxSpeed ? _maxSpeed : offsetY; - RunInUIThread(dispatcherQueue, () => - { - _scrollViewer?.ChangeView(_scrollViewer.HorizontalOffset + offsetX, _scrollViewer.VerticalOffset + offsetY, null, true); - }); + dispatcherQueue.EnqueueAsync(() => _scrollViewer?.ChangeView(_scrollViewer.HorizontalOffset + offsetX, _scrollViewer.VerticalOffset + offsetY, null, true)); } } @@ -326,10 +323,7 @@ private static void SetCursorType(DispatcherQueue dispatcherQueue, double offset if (_oldCursorID != cursorID) { - RunInUIThread(dispatcherQueue, () => - { - Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Custom, cursorID); - }); + dispatcherQueue.EnqueueAsync(() => Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Custom, cursorID)); _oldCursorID = cursorID; } @@ -366,10 +360,5 @@ private static bool IsCursorResourceAvailable() return isCursorAvailable; } - - private static async void RunInUIThread(DispatcherQueue dispatcherQueue, Action action) - { - await dispatcherQueue.EnqueueAsync(action, DispatcherQueuePriority.Normal); - } } } diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Gaze.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Gaze.cs index f8133b8be71..ed2391b8336 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Gaze.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Gaze.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; +using Microsoft.Toolkit.Uwp.Extensions; using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.Input.GazeInteraction; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -21,7 +22,7 @@ public partial class XamlIslandsTest_Gaze [TestInitialize] public async Task Init() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var xamlItemsPanelTemplate = @" [TestCleanup] public async Task Cleanup() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { GazeInput.SetInteraction(_grid, Interaction.Disabled); }); @@ -76,7 +77,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => [Ignore] public async Task Gaze_DoesNotCrashOnIslands() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { await Task.Delay(10000); From 8f3738d0bbb0543194d10bf6394c7e9e985d8b9a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 3 Dec 2020 20:25:11 +0100 Subject: [PATCH 2/2] Minor code tweaks --- .../Extensions/DispatcherQueueExtensions.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs index 29bfbdad8ac..cb61cd2120e 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using Windows.Foundation.Metadata; using Windows.System; @@ -67,7 +68,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Action function, Dispatc } })) { - taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation")); } return taskCompletionSource.Task; @@ -116,7 +117,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func function, Dis } })) { - taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation")); } return taskCompletionSource.Task; @@ -149,7 +150,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func func return awaitableResult; } - return Task.FromException(new InvalidOperationException("The Task returned by function cannot be null.")); + return Task.FromException(GetEnqueueException("The Task returned by function cannot be null.")); } catch (Exception e) { @@ -173,7 +174,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func function, Dis } else { - taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + taskCompletionSource.SetException(GetEnqueueException("The Task returned by function cannot be null.")); } } catch (Exception e) @@ -182,7 +183,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func function, Dis } })) { - taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation")); } return taskCompletionSource.Task; @@ -212,7 +213,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func(new InvalidOperationException("The Task returned by function cannot be null.")); + return Task.FromException(GetEnqueueException("The Task returned by function cannot be null.")); } catch (Exception e) { @@ -236,7 +237,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func> functio } else { - taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + taskCompletionSource.SetException(GetEnqueueException("The Task returned by function cannot be null.")); } } catch (Exception e) @@ -245,7 +246,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func> functio } })) { - taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation")); } return taskCompletionSource.Task; @@ -253,5 +254,16 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func> functio return TryEnqueueAsync(dispatcher, function, priority); } + + /// + /// Creates an to return when an enqueue operation fails. + /// + /// The message of the exception. + /// An with a specified message. + [MethodImpl(MethodImplOptions.NoInlining)] + private static InvalidOperationException GetEnqueueException(string message) + { + return new InvalidOperationException(message); + } } }