From 45a90faa213f574b48744a9339462aa73ed74e9c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 00:30:31 +0200 Subject: [PATCH 01/17] Removed unnecessary API presence check --- .../Helpers/DispatcherQueueHelper.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs index e752e17895a..f7c8ed28384 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs @@ -4,7 +4,6 @@ using System; using System.Threading.Tasks; -using Windows.Foundation.Metadata; using Windows.System; namespace Microsoft.Toolkit.Uwp.Helpers @@ -32,7 +31,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Actio /* Run the function directly when we have thread access. * Also reuse Task.CompletedTask in case of success, * to skip an unnecessary heap allocation for every invocation. */ - if (HasThreadAccess(dispatcher)) + if (dispatcher.HasThreadAccess) { try { @@ -81,7 +80,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, throw new ArgumentNullException(nameof(function)); } - if (HasThreadAccess(dispatcher)) + if (dispatcher.HasThreadAccess) { try { @@ -129,7 +128,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func< * We don't use ConfigureAwait(false) in this case, in order * to let the caller continue its execution on the same thread * after awaiting the task returned by this function. */ - if (HasThreadAccess(dispatcher)) + if (dispatcher.HasThreadAccess) { try { @@ -188,7 +187,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, throw new ArgumentNullException(nameof(function)); } - if (HasThreadAccess(dispatcher)) + if (dispatcher.HasThreadAccess) { try { @@ -230,10 +229,5 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, return taskCompletionSource.Task; } - - private static bool HasThreadAccess(DispatcherQueue dispatcher) - { - return ApiInformation.IsMethodPresent("Windows.System.DispatcherQueue", "HasThreadAccess") && dispatcher.HasThreadAccess; - } } } From 9f9ff7526cccb6081940114036b208e552cb5f27 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 00:31:15 +0200 Subject: [PATCH 02/17] Updated comments style --- .../Helpers/DispatcherQueueHelper.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs index f7c8ed28384..5d0bbb30fdc 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs @@ -28,9 +28,9 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Actio throw new ArgumentNullException(nameof(function)); } - /* Run the function directly when we have thread access. - * Also reuse Task.CompletedTask in case of success, - * to skip an unnecessary heap allocation for every invocation. */ + // Run the function directly when we have thread access. + // Also reuse Task.CompletedTask in case of success, + // to skip an unnecessary heap allocation for every invocation. if (dispatcher.HasThreadAccess) { try @@ -124,10 +124,10 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func< throw new ArgumentNullException(nameof(function)); } - /* If we have thread access, we can retrieve the task directly. - * We don't use ConfigureAwait(false) in this case, in order - * to let the caller continue its execution on the same thread - * after awaiting the task returned by this function. */ + // If we have thread access, we can retrieve the task directly. + // We don't use ConfigureAwait(false) in this case, in order + // to let the caller continue its execution on the same thread + // after awaiting the task returned by this function. if (dispatcher.HasThreadAccess) { try From 899af46e7ebbc61ec77a351baf23982e68d60430 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 00:33:44 +0200 Subject: [PATCH 03/17] Switched to Guard APIs --- .../Helpers/DispatcherQueueHelper.cs | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs index 5d0bbb30fdc..2a6fd7ff6de 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; +using Microsoft.Toolkit.Diagnostics; using Windows.System; namespace Microsoft.Toolkit.Uwp.Helpers @@ -23,10 +24,7 @@ public static class DispatcherQueueHelper /// If the current thread has UI access, will be invoked directly. public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - if (function is null) - { - throw new ArgumentNullException(nameof(function)); - } + Guard.IsNotNull(function, nameof(function)); // Run the function directly when we have thread access. // Also reuse Task.CompletedTask in case of success, @@ -75,10 +73,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Actio /// If the current thread has UI access, will be invoked directly. public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - if (function is null) - { - throw new ArgumentNullException(nameof(function)); - } + Guard.IsNotNull(function, nameof(function)); if (dispatcher.HasThreadAccess) { @@ -119,10 +114,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, /// If the current thread has UI access, will be invoked directly. public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - if (function is null) - { - throw new ArgumentNullException(nameof(function)); - } + Guard.IsNotNull(function, nameof(function)); // If we have thread access, we can retrieve the task directly. // We don't use ConfigureAwait(false) in this case, in order @@ -182,10 +174,7 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func< /// If the current thread has UI access, will be invoked directly. public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - if (function is null) - { - throw new ArgumentNullException(nameof(function)); - } + Guard.IsNotNull(function, nameof(function)); if (dispatcher.HasThreadAccess) { From f071d2b26a5576760d6b49a50d3d40cf4085700b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 00:48:13 +0200 Subject: [PATCH 04/17] Improved codegen, removed unnecessary allocations The previous version caused the C# compiler to always instantiate a display class for the closure even if the execution had thread access --- .../Helpers/DispatcherQueueHelper.cs | 138 ++++++++++-------- .../Microsoft.Toolkit.Uwp.csproj | 1 + 2 files changed, 80 insertions(+), 59 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs index 2a6fd7ff6de..1e69fe4beb9 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs @@ -43,23 +43,28 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Actio } } - var taskCompletionSource = new TaskCompletionSource(); - - _ = dispatcher.TryEnqueue(priority, () => + static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority) { - try - { - function(); + var taskCompletionSource = new TaskCompletionSource(); - taskCompletionSource.SetResult(null); - } - catch (Exception e) + _ = dispatcher.TryEnqueue(priority, () => { - taskCompletionSource.SetException(e); - } - }); + try + { + function(); + + taskCompletionSource.SetResult(null); + } + catch (Exception e) + { + taskCompletionSource.SetException(e); + } + }); - return taskCompletionSource.Task; + return taskCompletionSource.Task; + } + + return RunAsync(dispatcher, function, priority); } /// @@ -87,21 +92,26 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, } } - var taskCompletionSource = new TaskCompletionSource(); - - _ = dispatcher.TryEnqueue(priority, () => + static Task RunAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) { - try - { - taskCompletionSource.SetResult(function()); - } - catch (Exception e) + var taskCompletionSource = new TaskCompletionSource(); + + _ = dispatcher.TryEnqueue(priority, () => { - taskCompletionSource.SetException(e); - } - }); + try + { + taskCompletionSource.SetResult(function()); + } + catch (Exception e) + { + taskCompletionSource.SetException(e); + } + }); - return taskCompletionSource.Task; + return taskCompletionSource.Task; + } + + return RunAsync(dispatcher, function, priority); } /// @@ -137,30 +147,35 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func< } } - var taskCompletionSource = new TaskCompletionSource(); - - _ = dispatcher.TryEnqueue(priority, async () => + static Task RunAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) { - try + var taskCompletionSource = new TaskCompletionSource(); + + _ = dispatcher.TryEnqueue(priority, async () => { - if (function() is Task awaitableResult) + try { - await awaitableResult.ConfigureAwait(false); - - taskCompletionSource.SetResult(null); + if (function() is Task awaitableResult) + { + await awaitableResult.ConfigureAwait(false); + + taskCompletionSource.SetResult(null); + } + else + { + taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + } } - else + catch (Exception e) { - taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + taskCompletionSource.SetException(e); } - } - catch (Exception e) - { - taskCompletionSource.SetException(e); - } - }); + }); - return taskCompletionSource.Task; + return taskCompletionSource.Task; + } + + return RunAsync(dispatcher, function, priority); } /// @@ -193,30 +208,35 @@ public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, } } - var taskCompletionSource = new TaskCompletionSource(); - - _ = dispatcher.TryEnqueue(priority, async () => + static Task RunAsync(DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority) { - try + var taskCompletionSource = new TaskCompletionSource(); + + _ = dispatcher.TryEnqueue(priority, async () => { - if (function() is Task awaitableResult) + try { - var result = await awaitableResult.ConfigureAwait(false); - - taskCompletionSource.SetResult(result); + if (function() is Task awaitableResult) + { + var result = await awaitableResult.ConfigureAwait(false); + + taskCompletionSource.SetResult(result); + } + else + { + taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + } } - else + catch (Exception e) { - taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + taskCompletionSource.SetException(e); } - } - catch (Exception e) - { - taskCompletionSource.SetException(e); - } - }); + }); + + return taskCompletionSource.Task; + } - return taskCompletionSource.Task; + return RunAsync(dispatcher, function, priority); } } } diff --git a/Microsoft.Toolkit.Uwp/Microsoft.Toolkit.Uwp.csproj b/Microsoft.Toolkit.Uwp/Microsoft.Toolkit.Uwp.csproj index 07ef6adb800..9ba9b97cd85 100644 --- a/Microsoft.Toolkit.Uwp/Microsoft.Toolkit.Uwp.csproj +++ b/Microsoft.Toolkit.Uwp/Microsoft.Toolkit.Uwp.csproj @@ -5,6 +5,7 @@ Windows Community Toolkit This package includes code only helpers such as Colors conversion tool, Storage file handling, a Stream helper class, etc. UWP Toolkit Windows + 8.0 From 09977702ea0d75f69b40956dea4a63ff375e320b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 00:50:56 +0200 Subject: [PATCH 05/17] Renamed DispatcherQueueHelper APIs --- .../BluetoothLEHelper/BluetoothLEHelper.cs | 6 ++-- .../ObservableBluetoothLEDevice.cs | 10 +++--- .../ObservableGattCharacteristics.cs | 2 +- .../DispatcherQueueHelperPage.xaml.cs | 2 +- Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs | 2 +- ...llViewerExtensions.MiddleClickScrolling.cs | 2 +- .../Helpers/ThemeListener.cs | 2 +- .../Helpers/DispatcherQueueHelper.cs | 8 ++--- .../Helpers/PrintHelper/PrintHelper.cs | 10 +++--- .../PrintHelper/PrintHelperStateBag.cs | 2 +- .../RemoteDeviceHelper/RemoteDeviceHelper.cs | 6 ++-- .../Helpers/Test_DispatcherQueueHelper.cs | 34 +++++++++---------- .../Controls/Test_TextToolbar_Localization.cs | 2 +- .../TestsPage.xaml.cs | 4 +-- .../XamlIslandsTest_DropShadowPanel.cs | 4 +-- .../XamlIslandsTest_Eyedropper.cs | 4 +-- .../XamlIslandsTest_StringExtensions.cs | 10 +++--- .../XamlIslandsTest_SystemInformation.cs | 2 +- .../XamlIslandsTest_TextToolbar.cs | 4 +-- ...XamlIslandsTest_ThemeListener_Threading.cs | 2 +- .../XamlIslandsTest_TokenizingTextBox.cs | 6 ++-- .../XamlIslandsTest_WrapPanel.cs | 4 +-- 22 files changed, 64 insertions(+), 64 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs index c83718ec2ac..56aaaf6d15a 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs @@ -197,7 +197,7 @@ private async Task Init() /// The advertisement. private async void AdvertisementWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( () => { if (_readerWriterLockSlim.TryEnterReadLock(TimeSpan.FromSeconds(1))) @@ -281,7 +281,7 @@ private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformation // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == _deviceWatcher) { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( () => { if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1))) @@ -331,7 +331,7 @@ private async Task AddDeviceToList(DeviceInformation deviceInfo) if (connectable) { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( () => { if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1))) diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs index 2e72750c433..df682969ea3 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs @@ -404,7 +404,7 @@ private void ObservableBluetoothLEDevice_PropertyChanged(object sender, Property /// Throws Exception when no permission to access device public async Task ConnectAsync() { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( async () => { if (BluetoothLEDevice == null) @@ -478,7 +478,7 @@ public async Task DoInAppPairingAsync() /// The task of the update. public async Task UpdateAsync(DeviceInformationUpdate deviceUpdate) { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( () => { DeviceInfo.Update(deviceUpdate); @@ -521,7 +521,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName /// The arguments. private async void BluetoothLEDevice_NameChanged(BluetoothLEDevice sender, object args) { - await DispatcherQueue.ExecuteOnUIThreadAsync(() => { Name = BluetoothLEDevice.Name; }, DispatcherQueuePriority.Normal); + await DispatcherQueue.EnqueueAsync(() => { Name = BluetoothLEDevice.Name; }, DispatcherQueuePriority.Normal); } /// @@ -531,7 +531,7 @@ private async void BluetoothLEDevice_NameChanged(BluetoothLEDevice sender, objec /// The arguments. private async void BluetoothLEDevice_ConnectionStatusChanged(BluetoothLEDevice sender, object args) { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( () => { IsPaired = DeviceInfo.Pairing.IsPaired; @@ -544,7 +544,7 @@ await DispatcherQueue.ExecuteOnUIThreadAsync( /// private async void LoadGlyph() { - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( async () => { var deviceThumbnail = await DeviceInfo.GetGlyphThumbnailAsync(); diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs index c1ab661c904..ab57e5eafbb 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs @@ -469,7 +469,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName /// The instance containing the event data. private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { - await DispatcherQueue.ExecuteOnUIThreadAsync(() => { SetValue(args.CharacteristicValue); }, DispatcherQueuePriority.Normal); + await DispatcherQueue.EnqueueAsync(() => { SetValue(args.CharacteristicValue); }, DispatcherQueuePriority.Normal); } /// diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs index 7b39cccf51a..75c308b7974 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs @@ -21,7 +21,7 @@ private async void ExecuteFromDifferentThreadButton_Click(object sender, RoutedE var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); int crossThreadReturnedValue = await Task.Run(async () => { - int returnedFromUIThread = await dispatcherQueue.ExecuteOnUIThreadAsync(() => + int returnedFromUIThread = await dispatcherQueue.EnqueueAsync(() => { NormalTextBlock.Text = "Updated from a random thread!"; return 1; diff --git a/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs b/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs index ece261b30b7..85522ae039b 100644 --- a/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs +++ b/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs @@ -63,7 +63,7 @@ protected override async Task InitializeTypeAsync(Stream stream, Li throw new FileNotFoundException(); } - return await DispatcherQueue.ExecuteOnUIThreadAsync(async () => + return await DispatcherQueue.EnqueueAsync(async () => { BitmapImage image = new BitmapImage(); diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs index 9b956d7510a..1f2a13ab66e 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs @@ -369,7 +369,7 @@ private static bool IsCursorResourceAvailable() private static async void RunInUIThread(DispatcherQueue dispatcherQueue, Action action) { - await dispatcherQueue.ExecuteOnUIThreadAsync(action, DispatcherQueuePriority.Normal); + await dispatcherQueue.EnqueueAsync(action, DispatcherQueuePriority.Normal); } } } diff --git a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs index 9dc2559d506..cc41c024538 100644 --- a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs +++ b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs @@ -100,7 +100,7 @@ private async void Settings_ColorValuesChanged(UISettings sender, object args) internal Task OnColorValuesChanged() { // Getting called off thread, so we need to dispatch to request value. - return DispatcherQueue.ExecuteOnUIThreadAsync( + return DispatcherQueue.EnqueueAsync( () => { // TODO: This doesn't stop the multiple calls if we're in our faked 'White' HighContrast Mode below. diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs index 1e69fe4beb9..46f789ea91e 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs @@ -22,7 +22,7 @@ public static class DispatcherQueueHelper /// DispatcherQueue execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. - public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) + public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); @@ -76,7 +76,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueu /// DispatcherQueue execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. - public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) + public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); @@ -122,7 +122,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher /// DispatcherQueue execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. - public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) + public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); @@ -187,7 +187,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher /// DispatcherQueue execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. - public static Task ExecuteOnUIThreadAsync(this DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) + public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); diff --git a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs index 3e489433331..9e43b3c294b 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs @@ -205,7 +205,7 @@ public void Dispose() } _printCanvas = null; - DispatcherQueue.ExecuteOnUIThreadAsync(() => + DispatcherQueue.EnqueueAsync(() => { _printDocument.Paginate -= CreatePrintPreviewPages; _printDocument.GetPreviewPage -= GetPrintPreviewPage; @@ -229,7 +229,7 @@ private async Task DetachCanvas() { if (!_directPrint) { - await DispatcherQueue.ExecuteOnUIThreadAsync(() => + await DispatcherQueue.EnqueueAsync(() => { _canvasContainer.Children.Remove(_printCanvas); _printCanvas.Children.Clear(); @@ -262,7 +262,7 @@ private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs printTask.Completed += async (s, args) => { // Notify the user when the print operation fails. - await DispatcherQueue.ExecuteOnUIThreadAsync( + await DispatcherQueue.EnqueueAsync( async () => { foreach (var element in _stateBags.Keys) @@ -515,7 +515,7 @@ private Task AddOnePrintPreviewPage(FrameworkElement element, PrintPageDescripti element.Margin = new Thickness(marginWidth / 2, marginHeight / 2, marginWidth / 2, marginHeight / 2); page.Content = element; - return DispatcherQueue.ExecuteOnUIThreadAsync( + return DispatcherQueue.EnqueueAsync( () => { // Add the (newly created) page to the print canvas which is part of the visual tree and force it to go @@ -531,7 +531,7 @@ private Task AddOnePrintPreviewPage(FrameworkElement element, PrintPageDescripti private Task ClearPageCache() { - return DispatcherQueue.ExecuteOnUIThreadAsync(() => + return DispatcherQueue.EnqueueAsync(() => { if (!_directPrint) { diff --git a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs index 7be4a651451..e69eda226e3 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs @@ -63,7 +63,7 @@ public void Capture(FrameworkElement element) /// Element to restore state to public void Restore(FrameworkElement element) { - _dispatcherQueue.ExecuteOnUIThreadAsync(() => + _dispatcherQueue.EnqueueAsync(() => { element.HorizontalAlignment = HorizontalAlignment; element.VerticalAlignment = VerticalAlignment; diff --git a/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs index 842176428c4..e7ea571a171 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs @@ -84,7 +84,7 @@ private void RemoteSystemWatcher_EnumerationCompleted(RemoteSystemWatcher sender private async void RemoteSystemWatcher_RemoteSystemUpdated(RemoteSystemWatcher sender, RemoteSystemUpdatedEventArgs args) { - await DispatcherQueue.ExecuteOnUIThreadAsync(() => + await DispatcherQueue.EnqueueAsync(() => { RemoteSystems.Remove(RemoteSystems.First(a => a.Id == args.RemoteSystem.Id)); RemoteSystems.Add(args.RemoteSystem); @@ -93,7 +93,7 @@ await DispatcherQueue.ExecuteOnUIThreadAsync(() => private async void RemoteSystemWatcher_RemoteSystemRemoved(RemoteSystemWatcher sender, RemoteSystemRemovedEventArgs args) { - await DispatcherQueue.ExecuteOnUIThreadAsync(() => + await DispatcherQueue.EnqueueAsync(() => { RemoteSystems.Remove(RemoteSystems.First(a => a.Id == args.RemoteSystemId)); }); @@ -101,7 +101,7 @@ await DispatcherQueue.ExecuteOnUIThreadAsync(() => private async void RemoteSystemWatcher_RemoteSystemAdded(RemoteSystemWatcher sender, RemoteSystemAddedEventArgs args) { - await DispatcherQueue.ExecuteOnUIThreadAsync(() => + await DispatcherQueue.EnqueueAsync(() => { RemoteSystems.Add(args.RemoteSystem); }); diff --git a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs b/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs index ba81bb1afc6..d53ab8ff712 100644 --- a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs +++ b/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs @@ -27,14 +27,14 @@ public class Test_DispatcherQueueHelper [ExpectedException(typeof(ArgumentNullException))] public void Test_DispatcherQueueHelper_Action_Null() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), default(Action)); + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Action)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_Action_Ok_UIThread() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_UIThread) }; @@ -55,7 +55,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); await Task.Run(async () => { - await DispatcherQueueHelper.ExecuteOnUIThreadAsync(dispatcherQueue, () => + await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_NonUIThread) }; @@ -77,7 +77,7 @@ await DispatcherQueueHelper.ExecuteOnUIThreadAsync(dispatcherQueue, () => [UITestMethod] public void Test_DispatcherQueueHelper_Action_Exception() { - var task = DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), () => + var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_Action_Exception)); }); @@ -93,14 +93,14 @@ public void Test_DispatcherQueueHelper_Action_Exception() [ExpectedException(typeof(ArgumentNullException))] public void Test_DispatcherQueueHelper_FuncOfT_Null() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread() { - var textBlock = DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), () => + var textBlock = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread) }; }).Result; @@ -121,11 +121,11 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); await Task.Run(async () => { - var textBlock = await DispatcherQueueHelper.ExecuteOnUIThreadAsync(dispatcherQueue, () => + var textBlock = await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => { return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread) }; }); - await DispatcherQueueHelper.ExecuteOnUIThreadAsync(dispatcherQueue, () => + await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => { Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread)); taskSource.SetResult(null); @@ -144,7 +144,7 @@ await DispatcherQueueHelper.ExecuteOnUIThreadAsync(dispatcherQueue, () => [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfT_Exception() { - var task = DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => + var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfT_Exception)); })); @@ -161,14 +161,14 @@ public void Test_DispatcherQueueHelper_FuncOfT_Exception() [SuppressMessage("Style", "IDE0034", Justification = "Explicit overload for clarity")] public void Test_DispatcherQueueHelper_FuncOfTask_Null() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread) }; @@ -188,7 +188,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( { try { - await DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), async () => + await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => { await Task.Yield(); @@ -211,7 +211,7 @@ await DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrent [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTask_Exception() { - var task = DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => + var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTask_Exception)); })); @@ -227,14 +227,14 @@ public void Test_DispatcherQueueHelper_FuncOfTask_Exception() [ExpectedException(typeof(ArgumentNullException))] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Null() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), default(Func>)); + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func>)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread() { - DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread) }; @@ -254,7 +254,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( { try { - await DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), async () => + await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => { await Task.Yield(); @@ -279,7 +279,7 @@ await DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrent [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception() { - var task = DispatcherQueueHelper.ExecuteOnUIThreadAsync(DispatcherQueue.GetForCurrentThread(), new Func>(() => + var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func>(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception)); })); diff --git a/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs b/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs index e4acdf40096..c8b669a12b5 100644 --- a/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs +++ b/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs @@ -78,7 +78,7 @@ public void Test_TextToolbar_Localization_Override() [TestMethod] public async Task Test_TextToolbar_Localization_Override_Fr() { - await CoreApplication.MainView.DispatcherQueue.ExecuteOnUIThreadAsync(async () => + await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async () => { // Just double-check we've got the right environment setup in our tests. CollectionAssert.AreEquivalent(new string[] { "en-US", "fr" }, ApplicationLanguages.ManifestLanguages.ToArray(), "Missing locales for test"); diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs index 34754312ce1..de6270cc151 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs @@ -147,7 +147,7 @@ private async Task RunTestsAsync(Type type) } finally { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { SetMainTestContent(null); }); @@ -161,7 +161,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => private Task WriteLineAsync(string message, Color? color = null, bool deleteLastLine = false) { Debug.WriteLine(message); - return App.Dispatcher.ExecuteOnUIThreadAsync(() => + return App.Dispatcher.EnqueueAsync(() => { if (deleteLastLine) { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs index be3a7438b71..397c3dcabf5 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs @@ -19,7 +19,7 @@ public partial class XamlIslandsTest_DropShadowPanel [TestInitialize] public async Task Init() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { _dropShadowPanel = new DropShadowPanel { @@ -39,7 +39,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => [TestMethod] public async Task DropShadowPanel_RendersFine() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { var textBlock = new TextBlock { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs index aa18eaf7b3a..47943e37a22 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs @@ -20,7 +20,7 @@ public async Task Eyedropper_DoesntCrash() { Eyedropper eyedropper = null; Color? color = null; - _ = App.Dispatcher.ExecuteOnUIThreadAsync(async () => + _ = App.Dispatcher.EnqueueAsync(async () => { eyedropper = new Eyedropper { @@ -29,7 +29,7 @@ public async Task Eyedropper_DoesntCrash() color = await eyedropper.Open(); }); - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { var xamlRoot = App.XamlRoot; diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_StringExtensions.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_StringExtensions.cs index fd81c47a93b..d9393b8054c 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_StringExtensions.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_StringExtensions.cs @@ -22,7 +22,7 @@ public partial class XamlIslandsTest_StringExtensions [TestMethod] public async Task StringExtensions_GetViewLocalized() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var xamlRoot = App.XamlRoot; var str = StringExtensions.GetViewLocalized("abc", xamlRoot.UIContext); @@ -33,7 +33,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => [TestMethod] public async Task StringExtensions_GetLocalized() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var xamlRoot = App.XamlRoot; var str = "abc".GetLocalized(xamlRoot.UIContext); @@ -55,7 +55,7 @@ public void StringExtensions_GetLocalizedWithResourcePath() [TestMethod] public async Task Test_TextToolbar_Localization_Retrieve() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var treeRoot = XamlReader.Load( @" [TestMethod] public async Task Test_TextToolbar_Localization_Override() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var commonButtons = new CommonButtons(new TextToolbar()); var italicsButton = commonButtons.Italics; @@ -109,7 +109,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => [TestMethod] public async Task Test_TextToolbar_Localization_Override_Fr() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { // Just double-check we've got the right environment setup in our tests. //// Note: This seems to fail on XAML Islands, but the rest of the test works fine...? diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs index 668da199a41..1185337a970 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs @@ -15,7 +15,7 @@ public partial class XamlIslandsTest_SystemInformation [TestMethod] public async Task SystemInformationTrackAppUse() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var e = new FakeArgs { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs index e46d2546942..14a0f7e8d55 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs @@ -19,7 +19,7 @@ public partial class XamlIslandsTest_TextToolbar [TestInitialize] public async Task Init() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var richEditBox = new RichEditBox { @@ -62,7 +62,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => [TestMethod] public async Task TextToobar_PopupShowsInCorrectXamlRoot() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { await Task.Delay(500); diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs index aa3d98da9f5..19ffd716289 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs @@ -19,7 +19,7 @@ public partial class XamlIslandsTest_ThemeListener_Threading [TestInitialize] public Task Init() { - return App.Dispatcher.ExecuteOnUIThreadAsync(() => + return App.Dispatcher.EnqueueAsync(() => { _taskCompletionSource = new TaskCompletionSource(); diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs index dcb60f0f4bd..925349c1199 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs @@ -63,7 +63,7 @@ public override string ToString() [TestInitialize] public async Task Init() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { _acv = new AdvancedCollectionView(_samples, false); @@ -110,7 +110,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() => [TestMethod] public async Task TokenizingTextBox_GetFocusedElement_RemoveAllSelectedTokens() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { await Task.Delay(500); @@ -135,7 +135,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(async () => [TestMethod] public async Task TokenizingTextBox_PopupShowsInCorrectXamlRoot() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { await Task.Delay(500); diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs index d6e071e69e0..910b9b824f7 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs @@ -20,7 +20,7 @@ public partial class XamlIslandsTest_WrapPanel [TestInitialize] public async Task Init() { - await App.Dispatcher.ExecuteOnUIThreadAsync(() => + await App.Dispatcher.EnqueueAsync(() => { var xamlItemsPanelTemplate = @" [TestMethod] public async Task WrapPanel_RendersFine() { - await App.Dispatcher.ExecuteOnUIThreadAsync(async () => + await App.Dispatcher.EnqueueAsync(async () => { var item = new Uri("ms-appx:///Assets/StoreLogo.png"); for (int i = 0; i < 100; i++) From 288e0c75bf128456c9232781612344d03cb5136b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 00:53:00 +0200 Subject: [PATCH 06/17] Added handling for unsuccessful queueing --- .../Helpers/DispatcherQueueHelper.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs index 46f789ea91e..d107124cf0e 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs @@ -47,7 +47,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueu { var taskCompletionSource = new TaskCompletionSource(); - _ = dispatcher.TryEnqueue(priority, () => + if (!dispatcher.TryEnqueue(priority, () => { try { @@ -59,7 +59,10 @@ static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueu { taskCompletionSource.SetException(e); } - }); + })) + { + taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + } return taskCompletionSource.Task; } @@ -96,7 +99,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher { var taskCompletionSource = new TaskCompletionSource(); - _ = dispatcher.TryEnqueue(priority, () => + if (!dispatcher.TryEnqueue(priority, () => { try { @@ -106,7 +109,10 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher { taskCompletionSource.SetException(e); } - }); + })) + { + taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + } return taskCompletionSource.Task; } @@ -151,7 +157,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher { var taskCompletionSource = new TaskCompletionSource(); - _ = dispatcher.TryEnqueue(priority, async () => + if (!dispatcher.TryEnqueue(priority, async () => { try { @@ -170,7 +176,10 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher { taskCompletionSource.SetException(e); } - }); + })) + { + taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + } return taskCompletionSource.Task; } @@ -212,7 +221,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func> function, Disp { var taskCompletionSource = new TaskCompletionSource(); - _ = dispatcher.TryEnqueue(priority, async () => + if (!dispatcher.TryEnqueue(priority, async () => { try { @@ -231,7 +240,10 @@ static Task RunAsync(DispatcherQueue dispatcher, Func> function, Disp { taskCompletionSource.SetException(e); } - }); + })) + { + taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation")); + } return taskCompletionSource.Task; } From 83d84753b4b33b3d0651d094a41945422a546baa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 01:01:23 +0200 Subject: [PATCH 07/17] Code refactoring, improved XML docs --- .../BluetoothLEHelper/BluetoothLEHelper.cs | 1 + .../ObservableBluetoothLEDevice.cs | 1 + .../ObservableGattCharacteristics.cs | 1 + .../DispatcherQueueHelperPage.xaml.cs | 1 + Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs | 1 + ...llViewerExtensions.MiddleClickScrolling.cs | 1 + .../Helpers/ThemeListener.cs | 1 + .../DispatcherQueueExtensions.cs} | 62 ++++++++++--------- .../Helpers/PrintHelper/PrintHelper.cs | 1 + .../PrintHelper/PrintHelperStateBag.cs | 1 + .../RemoteDeviceHelper/RemoteDeviceHelper.cs | 1 + .../Helpers/Test_DispatcherQueueHelper.cs | 35 ++++++----- .../Controls/Test_TextToolbar_Localization.cs | 1 + .../TestsPage.xaml.cs | 1 + .../XamlIslandsTest_DropShadowPanel.cs | 1 + .../XamlIslandsTest_Eyedropper.cs | 1 + .../XamlIslandsTest_SystemInformation.cs | 1 + .../XamlIslandsTest_TextToolbar.cs | 1 + ...XamlIslandsTest_ThemeListener_Threading.cs | 1 + .../XamlIslandsTest_TokenizingTextBox.cs | 1 + .../XamlIslandsTest_WrapPanel.cs | 1 + 21 files changed, 70 insertions(+), 46 deletions(-) rename Microsoft.Toolkit.Uwp/{Helpers/DispatcherQueueHelper.cs => Extensions/DispatcherQueueExtensions.cs} (69%) diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs index 56aaaf6d15a..bdc8b02cc08 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs @@ -13,6 +13,7 @@ using Windows.Devices.Bluetooth.Advertisement; using Windows.Devices.Enumeration; using Windows.System; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Connectivity { diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs index df682969ea3..33d08ff6497 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs @@ -18,6 +18,7 @@ using Windows.System; using Windows.UI.Core; using Windows.UI.Xaml.Media.Imaging; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Connectivity { diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs index ab57e5eafbb..e41bebf3dc1 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs @@ -13,6 +13,7 @@ using Windows.Security.Cryptography; using Windows.Storage.Streams; using Windows.System; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Connectivity { diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs index 75c308b7974..fe61f6ea479 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs @@ -6,6 +6,7 @@ using Microsoft.Toolkit.Uwp.Helpers; using Windows.System; using Windows.UI.Xaml; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages { diff --git a/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs b/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs index 85522ae039b..e4a0c74e8ba 100644 --- a/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs +++ b/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs @@ -12,6 +12,7 @@ using Windows.Storage.Streams; using Windows.System; using Windows.UI.Xaml.Media.Imaging; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.UI { diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs index 1f2a13ab66e..9ffbb51ec50 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs @@ -13,6 +13,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.UI.Extensions { diff --git a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs index cc41c024538..0fcab85e53f 100644 --- a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs +++ b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs @@ -11,6 +11,7 @@ using Windows.System; using Windows.UI.ViewManagement; using Windows.UI.Xaml; +using Microsoft.Toolkit.Uwp.Extensions; [assembly: InternalsVisibleTo("UnitTests.XamlIslands.UWPApp")] diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs similarity index 69% rename from Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs rename to Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs index d107124cf0e..47ac376f304 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherQueueHelper.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs @@ -7,21 +7,22 @@ using Microsoft.Toolkit.Diagnostics; using Windows.System; -namespace Microsoft.Toolkit.Uwp.Helpers +namespace Microsoft.Toolkit.Uwp.Extensions { /// - /// This class provides static methods helper for executing code in a DispatcherQueue. + /// Helpers for executing code in a . /// - public static class DispatcherQueueHelper + public static class DispatcherQueueExtensions { /// - /// Extension method for . Offering an actual awaitable with optional result that will be executed on the given dispatcher. + /// Invokes a given function on the target and returns a + /// that completes when the invocation of the function is completed. /// - /// DispatcherQueue of a thread to run . - /// Function to be executed on the given dispatcher. - /// DispatcherQueue execution priority, default is normal. - /// An awaitable for the operation. - /// If the current thread has UI access, will be invoked directly. + /// The target to invoke the code on. + /// The to invoke. + /// The priority level for the function to invoke. + /// A that completes when the invocation of is over. + /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); @@ -71,14 +72,15 @@ static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueu } /// - /// Extension method for . Offering an actual awaitable with optional result that will be executed on the given dispatcher. + /// Invokes a given function on the target and returns a + /// that completes when the invocation of the function is completed. /// - /// Returned data type of the function. - /// DispatcherQueue of a thread to run . - /// Function to be executed on the given dispatcher. - /// DispatcherQueue execution priority, default is normal. - /// An awaitable for the operation. - /// If the current thread has UI access, will be invoked directly. + /// The return type of to relay through the returned . + /// The target to invoke the code on. + /// The to invoke. + /// The priority level for the function to invoke. + /// A that completes when the invocation of is over. + /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); @@ -121,13 +123,14 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher } /// - /// Extension method for . Offering an actual awaitable with optional result that will be executed on the given dispatcher. + /// Invokes a given function on the target and returns a + /// that acts as a proxy for the one returned by the given function. /// - /// DispatcherQueue of a thread to run . - /// Asynchronous function to be executed on the given dispatcher. - /// DispatcherQueue execution priority, default is normal. - /// An awaitable for the operation. - /// If the current thread has UI access, will be invoked directly. + /// The target to invoke the code on. + /// The to invoke. + /// The priority level for the function to invoke. + /// A that acts as a proxy for the one returned by . + /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); @@ -188,14 +191,15 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher } /// - /// Extension method for . Offering an actual awaitable with optional result that will be executed on the given dispatcher. + /// Invokes a given function on the target and returns a + /// that acts as a proxy for the one returned by the given function. /// - /// Returned data type of the function. - /// DispatcherQueue of a thread to run . - /// Asynchronous function to be executed Asynchronously on the given dispatcher. - /// DispatcherQueue execution priority, default is normal. - /// An awaitable for the operation. - /// If the current thread has UI access, will be invoked directly. + /// The return type of to relay through the returned . + /// The target to invoke the code on. + /// The to invoke. + /// The priority level for the function to invoke. + /// A that relays the one returned by . + /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { Guard.IsNotNull(function, nameof(function)); diff --git a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs index 9e43b3c294b..7a3810d257d 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs @@ -11,6 +11,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Printing; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Helpers { diff --git a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs index e69eda226e3..594d8c01320 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs @@ -4,6 +4,7 @@ using Windows.System; using Windows.UI.Xaml; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Helpers { diff --git a/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs index e7ea571a171..cd76986daaa 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs @@ -8,6 +8,7 @@ using System.Linq; using Windows.System; using Windows.System.RemoteSystems; +using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Helpers { diff --git a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs b/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs index d53ab8ff712..b40bbffb16a 100644 --- a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs +++ b/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs @@ -13,6 +13,7 @@ using Windows.ApplicationModel.Core; using Windows.UI.Core; using Windows.System; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.Helpers { @@ -27,14 +28,14 @@ public class Test_DispatcherQueueHelper [ExpectedException(typeof(ArgumentNullException))] public void Test_DispatcherQueueHelper_Action_Null() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Action)); + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Action)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_Action_Ok_UIThread() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_UIThread) }; @@ -55,7 +56,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); await Task.Run(async () => { - await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => + await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_NonUIThread) }; @@ -77,7 +78,7 @@ await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => [UITestMethod] public void Test_DispatcherQueueHelper_Action_Exception() { - var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_Action_Exception)); }); @@ -93,14 +94,14 @@ public void Test_DispatcherQueueHelper_Action_Exception() [ExpectedException(typeof(ArgumentNullException))] public void Test_DispatcherQueueHelper_FuncOfT_Null() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread() { - var textBlock = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + var textBlock = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread) }; }).Result; @@ -121,11 +122,11 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); await Task.Run(async () => { - var textBlock = await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => + var textBlock = await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => { return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread) }; }); - await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => + await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => { Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread)); taskSource.SetResult(null); @@ -144,7 +145,7 @@ await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () => [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfT_Exception() { - var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => + var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfT_Exception)); })); @@ -161,14 +162,14 @@ public void Test_DispatcherQueueHelper_FuncOfT_Exception() [SuppressMessage("Style", "IDE0034", Justification = "Explicit overload for clarity")] public void Test_DispatcherQueueHelper_FuncOfTask_Null() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread) }; @@ -188,7 +189,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( { try { - await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => + await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => { await Task.Yield(); @@ -211,7 +212,7 @@ await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTask_Exception() { - var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => + var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTask_Exception)); })); @@ -227,14 +228,14 @@ public void Test_DispatcherQueueHelper_FuncOfTask_Exception() [ExpectedException(typeof(ArgumentNullException))] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Null() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func>)); + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func>)); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread() { - DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread) }; @@ -254,7 +255,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( { try { - await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => + await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => { await Task.Yield(); @@ -279,7 +280,7 @@ await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception() { - var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func>(() => + var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func>(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception)); })); diff --git a/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs b/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs index c8b669a12b5..97de47cb3a9 100644 --- a/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs +++ b/UnitTests/UnitTests.UWP/UI/Controls/Test_TextToolbar_Localization.cs @@ -16,6 +16,7 @@ using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Markup; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.UI.Controls { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs index de6270cc151..16f85e4a620 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/TestsPage.xaml.cs @@ -14,6 +14,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Documents; using Windows.UI.Xaml.Media; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs index 397c3dcabf5..c76796ae762 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_DropShadowPanel.cs @@ -8,6 +8,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Windows.UI; using Windows.UI.Xaml.Controls; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs index 47943e37a22..ef71c417ac4 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Eyedropper.cs @@ -9,6 +9,7 @@ using Windows.Devices.Input; using Windows.Foundation; using Windows.UI; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs index 1185337a970..fdab8ffafb0 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_SystemInformation.cs @@ -6,6 +6,7 @@ using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using Windows.ApplicationModel.Activation; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs index 14a0f7e8d55..4ae1936d4d6 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TextToolbar.cs @@ -8,6 +8,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs index 19ffd716289..766a5f95142 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_ThemeListener_Threading.cs @@ -7,6 +7,7 @@ using Microsoft.Toolkit.Uwp.UI.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using Windows.UI.Xaml; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs index 925349c1199..6b4b304f6f8 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_TokenizingTextBox.cs @@ -10,6 +10,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Markup; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { diff --git a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs index 910b9b824f7..fb54c623398 100644 --- a/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs +++ b/UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_WrapPanel.cs @@ -9,6 +9,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Markup; +using Microsoft.Toolkit.Uwp.Extensions; namespace UnitTests.XamlIslands.UWPApp { From b70fb636f8d859c1d58489e3b54f3f3cd86b4fa8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 01:02:20 +0200 Subject: [PATCH 08/17] Removed redundant checks, added nullability annotations --- .../Extensions/DispatcherQueueExtensions.cs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs index 47ac376f304..57911bf1af6 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs @@ -4,9 +4,10 @@ using System; using System.Threading.Tasks; -using Microsoft.Toolkit.Diagnostics; using Windows.System; +#nullable enable + namespace Microsoft.Toolkit.Uwp.Extensions { /// @@ -25,8 +26,6 @@ public static class DispatcherQueueExtensions /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - Guard.IsNotNull(function, nameof(function)); - // Run the function directly when we have thread access. // Also reuse Task.CompletedTask in case of success, // to skip an unnecessary heap allocation for every invocation. @@ -46,7 +45,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority) { - var taskCompletionSource = new TaskCompletionSource(); + var taskCompletionSource = new TaskCompletionSource(); if (!dispatcher.TryEnqueue(priority, () => { @@ -83,8 +82,6 @@ static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueu /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - Guard.IsNotNull(function, nameof(function)); - if (dispatcher.HasThreadAccess) { try @@ -133,8 +130,6 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - Guard.IsNotNull(function, nameof(function)); - // If we have thread access, we can retrieve the task directly. // We don't use ConfigureAwait(false) in this case, in order // to let the caller continue its execution on the same thread @@ -158,7 +153,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func func static Task RunAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) { - var taskCompletionSource = new TaskCompletionSource(); + var taskCompletionSource = new TaskCompletionSource(); if (!dispatcher.TryEnqueue(priority, async () => { @@ -202,8 +197,6 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - Guard.IsNotNull(function, nameof(function)); - if (dispatcher.HasThreadAccess) { try From af5ef8ad97275c2fb36ba2d08054db9644fc64fc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 01:25:26 +0200 Subject: [PATCH 09/17] Renamed private methods --- .../Extensions/DispatcherQueueExtensions.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs index 57911bf1af6..e1b3fd9887d 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs @@ -43,7 +43,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function } } - static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority) + static Task TryEnqueueAsync(DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority) { var taskCompletionSource = new TaskCompletionSource(); @@ -67,7 +67,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Action function, DispatcherQueu return taskCompletionSource.Task; } - return RunAsync(dispatcher, function, priority); + return TryEnqueueAsync(dispatcher, function, priority); } /// @@ -94,7 +94,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func f } } - static Task RunAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) + static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) { var taskCompletionSource = new TaskCompletionSource(); @@ -116,7 +116,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher return taskCompletionSource.Task; } - return RunAsync(dispatcher, function, priority); + return TryEnqueueAsync(dispatcher, function, priority); } /// @@ -151,7 +151,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func func } } - static Task RunAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) + static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority) { var taskCompletionSource = new TaskCompletionSource(); @@ -182,7 +182,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func function, Dispatcher return taskCompletionSource.Task; } - return RunAsync(dispatcher, function, priority); + return TryEnqueueAsync(dispatcher, function, priority); } /// @@ -214,7 +214,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func RunAsync(DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority) + static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority) { var taskCompletionSource = new TaskCompletionSource(); @@ -245,7 +245,7 @@ static Task RunAsync(DispatcherQueue dispatcher, Func> function, Disp return taskCompletionSource.Task; } - return RunAsync(dispatcher, function, priority); + return TryEnqueueAsync(dispatcher, function, priority); } } } From d57c032008acea0c4797a21a21a4cbb5d43c29d1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 13:46:53 +0200 Subject: [PATCH 10/17] Fixed ordering of using directives --- Microsoft.Toolkit.Uwp/Deferred/EventDeferral.cs | 2 -- Microsoft.Toolkit.Uwp/Extensions/StringExtensions.cs | 1 - .../Helpers/DeepLinkParser/CollectionFormingDeepLinkParser.cs | 1 - Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs | 2 +- .../Helpers/PrintHelper/PrintHelperStateBag.cs | 2 +- .../Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs | 2 +- Microsoft.Toolkit.Uwp/Structures/OSVersion.cs | 2 -- 7 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Deferred/EventDeferral.cs b/Microsoft.Toolkit.Uwp/Deferred/EventDeferral.cs index b481c2854a7..6c9cb076603 100644 --- a/Microsoft.Toolkit.Uwp/Deferred/EventDeferral.cs +++ b/Microsoft.Toolkit.Uwp/Deferred/EventDeferral.cs @@ -3,8 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; -using System.Text; using System.Threading; using System.Threading.Tasks; diff --git a/Microsoft.Toolkit.Uwp/Extensions/StringExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/StringExtensions.cs index 8d97fe8b1fd..b19a592048c 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/StringExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/StringExtensions.cs @@ -4,7 +4,6 @@ using Windows.ApplicationModel.Resources; using Windows.UI; -using Windows.UI.WindowManagement; using Windows.UI.Xaml; namespace Microsoft.Toolkit.Uwp.Extensions diff --git a/Microsoft.Toolkit.Uwp/Helpers/DeepLinkParser/CollectionFormingDeepLinkParser.cs b/Microsoft.Toolkit.Uwp/Helpers/DeepLinkParser/CollectionFormingDeepLinkParser.cs index 8cf3acedd70..6e8426fed7e 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DeepLinkParser/CollectionFormingDeepLinkParser.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DeepLinkParser/CollectionFormingDeepLinkParser.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; using System.Linq; using Windows.ApplicationModel.Activation; diff --git a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs index 7a3810d257d..89414f089f5 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelper.cs @@ -6,12 +6,12 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Graphics.Printing; using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Printing; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Helpers { diff --git a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs index 594d8c01320..9c9b96d83e4 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/PrintHelper/PrintHelperStateBag.cs @@ -2,9 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Microsoft.Toolkit.Uwp.Extensions; using Windows.System; using Windows.UI.Xaml; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Helpers { diff --git a/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs index cd76986daaa..65635422234 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/RemoteDeviceHelper/RemoteDeviceHelper.cs @@ -6,9 +6,9 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.System; using Windows.System.RemoteSystems; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Helpers { diff --git a/Microsoft.Toolkit.Uwp/Structures/OSVersion.cs b/Microsoft.Toolkit.Uwp/Structures/OSVersion.cs index 9d3e0e8c628..5d00d94dca3 100644 --- a/Microsoft.Toolkit.Uwp/Structures/OSVersion.cs +++ b/Microsoft.Toolkit.Uwp/Structures/OSVersion.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Windows.System.Profile; - namespace Microsoft.Toolkit.Uwp.Helpers { /// From a5d19da315fb9c69e0902101728df9c28e47020c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 15:15:35 +0200 Subject: [PATCH 11/17] Fixed more using directives --- .../BluetoothLEHelper/BluetoothLEHelper.cs | 3 +-- .../BluetoothLEHelper/ObservableBluetoothLEDevice.cs | 4 +--- .../BluetoothLEHelper/ObservableGattCharacteristics.cs | 3 +-- Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs | 4 +--- .../ScrollViewerExtensions.MiddleClickScrolling.cs | 3 +-- Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs | 4 +--- 6 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs index bdc8b02cc08..bafe8f6939e 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/BluetoothLEHelper.cs @@ -8,12 +8,11 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.Advertisement; using Windows.Devices.Enumeration; using Windows.System; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Connectivity { diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs index 33d08ff6497..6aec998c966 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableBluetoothLEDevice.cs @@ -11,14 +11,12 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; using Windows.Devices.Enumeration; using Windows.System; -using Windows.UI.Core; using Windows.UI.Xaml.Media.Imaging; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Connectivity { diff --git a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs index e41bebf3dc1..e00b722967b 100644 --- a/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs +++ b/Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs @@ -7,13 +7,12 @@ using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; using Windows.Security.Cryptography; using Windows.Storage.Streams; using Windows.System; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.Connectivity { diff --git a/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs b/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs index e4a0c74e8ba..aef8f6f6a9a 100644 --- a/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs +++ b/Microsoft.Toolkit.Uwp.UI/Cache/ImageCache.cs @@ -7,12 +7,10 @@ using System.IO; using System.Reflection; using System.Threading.Tasks; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Storage; -using Windows.Storage.Streams; using Windows.System; using Windows.UI.Xaml.Media.Imaging; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.UI { diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs index 9ffbb51ec50..28bd87d9841 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs @@ -4,7 +4,7 @@ using System; using System.Threading; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Devices.Input; using Windows.Foundation; using Windows.System; @@ -13,7 +13,6 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; -using Microsoft.Toolkit.Uwp.Extensions; namespace Microsoft.Toolkit.Uwp.UI.Extensions { diff --git a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs index 0fcab85e53f..70a496eada2 100644 --- a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs +++ b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs @@ -3,15 +3,13 @@ // See the LICENSE file in the project root for more information. using System; -using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading.Tasks; -using Microsoft.Toolkit.Uwp.Helpers; +using Microsoft.Toolkit.Uwp.Extensions; using Windows.Foundation.Metadata; using Windows.System; using Windows.UI.ViewManagement; using Windows.UI.Xaml; -using Microsoft.Toolkit.Uwp.Extensions; [assembly: InternalsVisibleTo("UnitTests.XamlIslands.UWPApp")] From 73e190c09001a5c359aa052a06f7376f8541bda7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 16:11:25 +0200 Subject: [PATCH 12/17] Fixed build error in debug mode --- Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs index 70a496eada2..ab727617720 100644 --- a/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs +++ b/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs @@ -83,7 +83,7 @@ public ThemeListener(DispatcherQueue dispatcherQueue = null) private void Accessible_HighContrastChanged(AccessibilitySettings sender, object args) { #if DEBUG - Debug.WriteLine("HighContrast Changed"); + System.Diagnostics.Debug.WriteLine("HighContrast Changed"); #endif UpdateProperties(); @@ -107,7 +107,7 @@ internal Task OnColorValuesChanged() IsHighContrast != _accessible.HighContrast) { #if DEBUG - Debug.WriteLine("Color Values Changed"); + System.Diagnostics.Debug.WriteLine("Color Values Changed"); #endif UpdateProperties(); @@ -121,7 +121,7 @@ private void CoreWindow_Activated(Windows.UI.Core.CoreWindow sender, Windows.UI. IsHighContrast != _accessible.HighContrast) { #if DEBUG - Debug.WriteLine("CoreWindow Activated Changed"); + System.Diagnostics.Debug.WriteLine("CoreWindow Activated Changed"); #endif UpdateProperties(); From bb815454ef907eee312e9e2f5bfb967d1780a90c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 16:17:55 +0200 Subject: [PATCH 13/17] Code refactoring to unit tests --- .../Test_DispatcherQueueExtensions.cs} | 79 +++++++++---------- UnitTests/UnitTests.UWP/UnitTests.UWP.csproj | 2 +- 2 files changed, 39 insertions(+), 42 deletions(-) rename UnitTests/UnitTests.UWP/{Helpers/Test_DispatcherQueueHelper.cs => Extensions/Test_DispatcherQueueExtensions.cs} (76%) diff --git a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueExtensions.cs similarity index 76% rename from UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs rename to UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueExtensions.cs index b40bbffb16a..a032b0b78bf 100644 --- a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherQueueHelper.cs +++ b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueExtensions.cs @@ -3,39 +3,37 @@ // See the LICENSE file in the project root for more information. using System; -using System.Diagnostics.CodeAnalysis; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; using System.Linq; using System.Threading.Tasks; using Windows.UI.Xaml.Controls; -using Microsoft.Toolkit.Uwp.Helpers; using Windows.ApplicationModel.Core; using Windows.UI.Core; using Windows.System; using Microsoft.Toolkit.Uwp.Extensions; -namespace UnitTests.Helpers +namespace UnitTests.Extensions { [TestClass] [Ignore("Ignored until issue on .Net Native is fixed. These are working.")] - public class Test_DispatcherQueueHelper + public class Test_DispatcherQueueExtensions { private const int TIME_OUT = 5000; - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(NullReferenceException))] public void Test_DispatcherQueueHelper_Action_Null() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Action)); + DispatcherQueue.GetForCurrentThread().EnqueueAsync(default(Action)!); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_Action_Ok_UIThread() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueue.GetForCurrentThread().EnqueueAsync(() => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_UIThread) }; @@ -43,7 +41,7 @@ public void Test_DispatcherQueueHelper_Action_Ok_UIThread() }).Wait(); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [TestMethod] public async Task Test_DispatcherQueueHelper_Action_Ok_NonUIThread() { @@ -56,7 +54,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); await Task.Run(async () => { - await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => + await dispatcherQueue.EnqueueAsync(() => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_NonUIThread) }; @@ -74,11 +72,11 @@ await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => await taskSource.Task; } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_Action_Exception() { - var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + var task = DispatcherQueue.GetForCurrentThread().EnqueueAsync(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_Action_Exception)); }); @@ -89,19 +87,19 @@ public void Test_DispatcherQueueHelper_Action_Exception() Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException)); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(NullReferenceException))] public void Test_DispatcherQueueHelper_FuncOfT_Null() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); + DispatcherQueue.GetForCurrentThread().EnqueueAsync(default(Func)!); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread() { - var textBlock = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + var textBlock = DispatcherQueue.GetForCurrentThread().EnqueueAsync(() => { return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread) }; }).Result; @@ -109,7 +107,7 @@ public void Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread() Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread)); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [TestMethod] public async Task Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread() { @@ -122,11 +120,11 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); await Task.Run(async () => { - var textBlock = await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => + var textBlock = await dispatcherQueue.EnqueueAsync(() => { return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread) }; }); - await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => + await dispatcherQueue.EnqueueAsync(() => { Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread)); taskSource.SetResult(null); @@ -141,11 +139,11 @@ await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () => await taskSource.Task; } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfT_Exception() { - var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => + var task = DispatcherQueue.GetForCurrentThread().EnqueueAsync(new Func(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfT_Exception)); })); @@ -156,20 +154,19 @@ public void Test_DispatcherQueueHelper_FuncOfT_Exception() Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException)); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] - [ExpectedException(typeof(ArgumentNullException))] - [SuppressMessage("Style", "IDE0034", Justification = "Explicit overload for clarity")] + [ExpectedException(typeof(NullReferenceException))] public void Test_DispatcherQueueHelper_FuncOfTask_Null() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func)); + DispatcherQueue.GetForCurrentThread().EnqueueAsync(default(Func)!); } [TestCategory("Helpers")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueue.GetForCurrentThread().EnqueueAsync(() => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread) }; @@ -179,7 +176,7 @@ public void Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread() }).Wait(); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [TestMethod] public async Task Test_DispatcherQueueHelper_FuncOfTask_Ok_NonUIThread() { @@ -189,7 +186,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( { try { - await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => + await DispatcherQueue.GetForCurrentThread().EnqueueAsync(async () => { await Task.Yield(); @@ -208,11 +205,11 @@ await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread await taskSource.Task; } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTask_Exception() { - var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func(() => + var task = DispatcherQueue.GetForCurrentThread().EnqueueAsync(new Func(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTask_Exception)); })); @@ -223,19 +220,19 @@ public void Test_DispatcherQueueHelper_FuncOfTask_Exception() Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException)); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(NullReferenceException))] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Null() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func>)); + DispatcherQueue.GetForCurrentThread().EnqueueAsync(default(Func>)!); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread() { - DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () => + DispatcherQueue.GetForCurrentThread().EnqueueAsync(() => { var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread) }; @@ -245,7 +242,7 @@ public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread() }).Wait(); } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [TestMethod] public async Task Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_NonUIThread() { @@ -255,7 +252,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( { try { - await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () => + await DispatcherQueue.GetForCurrentThread().EnqueueAsync(async () => { await Task.Yield(); @@ -276,11 +273,11 @@ await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread await taskSource.Task; } - [TestCategory("Helpers")] + [TestCategory("DispatcherQueueExtensions")] [UITestMethod] public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception() { - var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func>(() => + var task = DispatcherQueue.GetForCurrentThread().EnqueueAsync(new Func>(() => { throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception)); })); diff --git a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj index 6ab314b8e48..0eb461d3888 100644 --- a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj +++ b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj @@ -146,7 +146,7 @@ - + From 25ed2d773aa77128a1991b38b2fcae180462aee8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 18:52:43 +0200 Subject: [PATCH 14/17] Added cached API check for SDK 17763 --- .../Extensions/DispatcherQueueExtensions.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs index e1b3fd9887d..29bfbdad8ac 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; +using Windows.Foundation.Metadata; using Windows.System; #nullable enable @@ -15,6 +16,11 @@ namespace Microsoft.Toolkit.Uwp.Extensions /// public static class DispatcherQueueExtensions { + /// + /// Indicates whether or not is available. + /// + private static readonly bool IsHasThreadAccessPropertyAvailable = ApiInformation.IsMethodPresent("Windows.System.DispatcherQueue", "HasThreadAccess"); + /// /// Invokes a given function on the target and returns a /// that completes when the invocation of the function is completed. @@ -29,7 +35,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function // Run the function directly when we have thread access. // Also reuse Task.CompletedTask in case of success, // to skip an unnecessary heap allocation for every invocation. - if (dispatcher.HasThreadAccess) + if (IsHasThreadAccessPropertyAvailable && dispatcher.HasThreadAccess) { try { @@ -82,7 +88,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Action function, Dispatc /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - if (dispatcher.HasThreadAccess) + if (IsHasThreadAccessPropertyAvailable && dispatcher.HasThreadAccess) { try { @@ -134,7 +140,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func func // We don't use ConfigureAwait(false) in this case, in order // to let the caller continue its execution on the same thread // after awaiting the task returned by this function. - if (dispatcher.HasThreadAccess) + if (IsHasThreadAccessPropertyAvailable && dispatcher.HasThreadAccess) { try { @@ -197,7 +203,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func function, Dis /// If the current thread has access to , will be invoked directly. public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { - if (dispatcher.HasThreadAccess) + if (IsHasThreadAccessPropertyAvailable && dispatcher.HasThreadAccess) { try { From eefd6b1e0107b30b214dc84b8a6524d9ad5f92b7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 23 Sep 2020 19:02:10 +0200 Subject: [PATCH 15/17] Added [Obsolete] messages to help migration --- Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs index c226d512edb..fd8b173dfd1 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs @@ -12,7 +12,7 @@ namespace Microsoft.Toolkit.Uwp.Helpers /// /// This class provides static methods helper for executing code in UI thread of the main window. /// - [Obsolete] + [Obsolete("Replace calls to APIs in this class with extensions for the Windows.System.DispatcherQueue type (see https://docs.microsoft.com/uwp/api/windows.system.dispatcherqueue).")] public static class DispatcherHelper { /// @@ -22,6 +22,7 @@ public static class DispatcherHelper /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -35,6 +36,7 @@ public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriorit /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -47,6 +49,7 @@ public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcher /// Asynchronous function to be executed asynchronously on UI thread. /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. + [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -60,6 +63,7 @@ public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPri /// Asynchronous function to be executed asynchronously on UI thread. /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. + [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -73,6 +77,7 @@ public static Task ExecuteOnUIThreadAsync(Func> function, CoreDisp /// Dispatcher execution priority, default is normal /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (viewToExecuteOn is null) @@ -92,6 +97,7 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecute /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (viewToExecuteOn is null) @@ -110,6 +116,7 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToE /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (viewToExecuteOn is null) @@ -129,6 +136,7 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecute /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")] public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (viewToExecuteOn is null) @@ -147,6 +155,7 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToE /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")] public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (function is null) @@ -199,6 +208,7 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action func /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")] public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (function is null) @@ -244,6 +254,7 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func< /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")] public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (function is null) @@ -307,6 +318,7 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")] public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (function is null) From 92b4fcaa908d7e281f23edbf1eb82422fe6b3f02 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 24 Sep 2020 13:39:06 +0200 Subject: [PATCH 16/17] Fixed build errors in test project --- UnitTests/UnitTests.UWP/Helpers/Test_DispatcherHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherHelper.cs b/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherHelper.cs index 94e37242419..5d0cb5e8a39 100644 --- a/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherHelper.cs +++ b/UnitTests/UnitTests.UWP/Helpers/Test_DispatcherHelper.cs @@ -13,7 +13,7 @@ namespace UnitTests.Helpers { -#pragma warning disable CS0612 // Type or member is obsolete +#pragma warning disable CS0612, CS0618 // Type or member is obsolete [TestClass] public class Test_DispatcherHelper { From 9ada6d9f385905a34c08e50140cd7c4c838120aa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Nov 2020 19:33:39 +0100 Subject: [PATCH 17/17] Updated [Obsolete] messages for DispatcherHelper --- Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs index fd8b173dfd1..217b1edc836 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs @@ -22,7 +22,7 @@ public static class DispatcherHelper /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. - [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")] public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -36,7 +36,7 @@ public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriorit /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. /// If the current thread has UI access, will be invoked directly. - [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")] public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -49,7 +49,7 @@ public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcher /// Asynchronous function to be executed asynchronously on UI thread. /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. - [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")] public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); @@ -63,7 +63,7 @@ public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPri /// Asynchronous function to be executed asynchronously on UI thread. /// Dispatcher execution priority, default is normal. /// An awaitable for the operation. - [Obsolete("This method should be replaced with CoreApplication.MainView.DispatcherQueue.EnqueueAsync(function, priority).")] + [Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")] public static Task ExecuteOnUIThreadAsync(Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);