From 2352a467eaa83ee1c7685be1fc58b2ef0748a9b1 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Wed, 10 Mar 2021 12:09:57 -0800 Subject: [PATCH 1/2] Moved a few things to use the DispatcherQueue. --- .../ConnectedAnimationHelper.cs | 23 +++--- .../DataGrid/DataGrid.cs | 2 + .../TokenizingTextBox.Selection.cs | 38 +++++----- .../TokenizingTextBox/TokenizingTextBox.cs | 71 ++++++++++--------- .../BladeView/BladeView.cs | 12 ++-- .../MarkdownTextBlock.Methods.cs | 5 +- .../Brushes/XamlCompositionBrush.cs | 15 ++-- .../Triggers/NetworkConnectionStateTrigger.cs | 6 +- 8 files changed, 93 insertions(+), 79 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Animations/ConnectedAnimations/ConnectedAnimationHelper.cs b/Microsoft.Toolkit.Uwp.UI.Animations/ConnectedAnimations/ConnectedAnimationHelper.cs index 35e33bb2f88..572b2775cb4 100644 --- a/Microsoft.Toolkit.Uwp.UI.Animations/ConnectedAnimations/ConnectedAnimationHelper.cs +++ b/Microsoft.Toolkit.Uwp.UI.Animations/ConnectedAnimations/ConnectedAnimationHelper.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Animation; @@ -149,17 +150,19 @@ void LoadedHandler(object s, RoutedEventArgs args) listAnimProperty.ListViewBase.ScrollIntoView(parameter); // give time to the UI thread to scroll the list - var t = listAnimProperty.ListViewBase.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => - { - try + var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + var t = dispatcherQueue.EnqueueAsync( + async () => { - var success = await listAnimProperty.ListViewBase.TryStartConnectedAnimationAsync(connectedAnimation, parameter, listAnimProperty.ElementName); - } - catch (Exception) - { - connectedAnimation.Cancel(); - } - }); + try + { + var success = await listAnimProperty.ListViewBase.TryStartConnectedAnimationAsync(connectedAnimation, parameter, listAnimProperty.ElementName); + } + catch (Exception) + { + connectedAnimation.Cancel(); + } + }, DispatcherQueuePriority.Normal); animationHandled = true; } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs index 13ed70989a1..6d1423d5202 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs @@ -7958,6 +7958,7 @@ private void ResetValidationStatus() int editingRowSlot = this.EditingRow.Slot; InvalidateMeasure(); + // TODO: Move to DispatcherQueue when FEATURE_VALIDATION_SUMMARY is enabled this.Dispatcher.BeginInvoke(() => { // It's possible that the DataContext or ItemsSource has changed by the time we reach this code, @@ -8762,6 +8763,7 @@ private void UpdateValidationResults(List newValidationResults // If the number of errors has changed, then the ValidationSummary will be a different size, // and we need to delay our call to ScrollSlotIntoView this.InvalidateMeasure(); + // TODO: Move to DispatcherQueue when FEATURE_VALIDATION_SUMMARY is enabled this.Dispatcher.BeginInvoke(() => { // It's possible that the DataContext or ItemsSource has changed by the time we reach this code, diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.Selection.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.Selection.cs index 32fa8776aa6..946162b24f9 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.Selection.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.Selection.cs @@ -5,7 +5,7 @@ using System; using System.Threading.Tasks; using Windows.ApplicationModel.DataTransfer; -using Windows.UI.Core; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; @@ -136,28 +136,30 @@ private TokenizingTextBoxItem GetCurrentContainerItem() internal void SelectAllTokensAndText() { - _ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => - { - this.SelectAllSafe(); + var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + _ = dispatcherQueue.EnqueueAsync( + () => + { + this.SelectAllSafe(); - // need to synchronize the select all and the focus behavior on the text box - // because there is no way to identify that the focus has been set from this point - // to avoid instantly clearing the selection of tokens - PauseTokenClearOnFocus = true; + // need to synchronize the select all and the focus behavior on the text box + // because there is no way to identify that the focus has been set from this point + // to avoid instantly clearing the selection of tokens + PauseTokenClearOnFocus = true; - foreach (var item in Items) - { - if (item is ITokenStringContainer) + foreach (var item in Items) { - // grab any selected text - var pretoken = ContainerFromItem(item) as TokenizingTextBoxItem; - pretoken._autoSuggestTextBox.SelectionStart = 0; - pretoken._autoSuggestTextBox.SelectionLength = pretoken._autoSuggestTextBox.Text.Length; + if (item is ITokenStringContainer) + { + // grab any selected text + var pretoken = ContainerFromItem(item) as TokenizingTextBoxItem; + pretoken._autoSuggestTextBox.SelectionStart = 0; + pretoken._autoSuggestTextBox.SelectionLength = pretoken._autoSuggestTextBox.Text.Length; + } } - } - (ContainerFromIndex(Items.Count - 1) as TokenizingTextBoxItem).Focus(FocusState.Programmatic); - }); + (ContainerFromIndex(Items.Count - 1) as TokenizingTextBoxItem).Focus(FocusState.Programmatic); + }, DispatcherQueuePriority.Normal); } internal void DeselectAllTokensAndText(TokenizingTextBoxItem ignoreItem = null) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs index 13091d9d89e..3a388c2b09d 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs @@ -224,54 +224,57 @@ private async void TokenizingTextBox_CharacterReceived(UIElement sender, Charact await RemoveAllSelectedTokens(); // Wait for removal of old items - _ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => - { - // If we're before the last textbox and it's empty, redirect focus to that one instead - if (index == _innerItemsSource.Count - 1 && string.IsNullOrWhiteSpace(_lastTextEdit.Text)) + var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + _ = dispatcherQueue.EnqueueAsync( + () => { - var lastContainer = ContainerFromItem(_lastTextEdit) as TokenizingTextBoxItem; + // If we're before the last textbox and it's empty, redirect focus to that one instead + if (index == _innerItemsSource.Count - 1 && string.IsNullOrWhiteSpace(_lastTextEdit.Text)) + { + var lastContainer = ContainerFromItem(_lastTextEdit) as TokenizingTextBoxItem; - lastContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items. + lastContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items. - _lastTextEdit.Text = string.Empty + args.Character; + _lastTextEdit.Text = string.Empty + args.Character; - UpdateCurrentTextEdit(_lastTextEdit); + UpdateCurrentTextEdit(_lastTextEdit); - lastContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted + lastContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted - lastContainer._autoSuggestTextBox.Focus(FocusState.Keyboard); - } - else - { - //// Otherwise, create a new textbox for this text. + lastContainer._autoSuggestTextBox.Focus(FocusState.Keyboard); + } + else + { + //// Otherwise, create a new textbox for this text. - UpdateCurrentTextEdit(new PretokenStringContainer((string.Empty + args.Character).Trim())); // Trim so that 'space' isn't inserted and can be used to insert a new box. + UpdateCurrentTextEdit(new PretokenStringContainer((string.Empty + args.Character).Trim())); // Trim so that 'space' isn't inserted and can be used to insert a new box. - _innerItemsSource.Insert(index, _currentTextEdit); + _innerItemsSource.Insert(index, _currentTextEdit); - // Need to wait for containerization - _ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => - { - var newContainer = ContainerFromIndex(index) as TokenizingTextBoxItem; // Should be our last text box + // Need to wait for containerization + _ = dispatcherQueue.EnqueueAsync( + () => + { + var newContainer = ContainerFromIndex(index) as TokenizingTextBoxItem; // Should be our last text box - newContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items. + newContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items. - void WaitForLoad(object s, RoutedEventArgs eargs) - { - if (newContainer._autoSuggestTextBox != null) - { - newContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted + void WaitForLoad(object s, RoutedEventArgs eargs) + { + if (newContainer._autoSuggestTextBox != null) + { + newContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted - newContainer._autoSuggestTextBox.Focus(FocusState.Keyboard); - } + newContainer._autoSuggestTextBox.Focus(FocusState.Keyboard); + } - newContainer.Loaded -= WaitForLoad; - } + newContainer.Loaded -= WaitForLoad; + } - newContainer.AutoSuggestTextBoxLoaded += WaitForLoad; - }); - } - }); + newContainer.AutoSuggestTextBoxLoaded += WaitForLoad; + }, DispatcherQueuePriority.Normal); + } + }, DispatcherQueuePriority.Normal); } else { diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Layout/BladeView/BladeView.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Layout/BladeView/BladeView.cs index 0548eced1ba..1b5b0b44228 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Layout/BladeView/BladeView.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Layout/BladeView/BladeView.cs @@ -9,7 +9,7 @@ using Microsoft.Toolkit.Uwp.UI.Automation.Peers; using Windows.Foundation; using Windows.Foundation.Collections; -using Windows.UI.Core; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Automation.Peers; using Windows.UI.Xaml.Controls; @@ -153,10 +153,12 @@ private async void BladeOnVisibilityChanged(object sender, Visibility visibility UpdateLayout(); // Need to do this because of touch. See more information here: https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/760#issuecomment-276466464 - await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => - { - GetScrollViewer()?.ChangeView(_scrollViewer.ScrollableWidth, null, null); - }); + var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + await dispatcherQueue.EnqueueAsync( + () => + { + GetScrollViewer()?.ChangeView(_scrollViewer.ScrollableWidth, null, null); + }, DispatcherQueuePriority.Low); return; } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Markdown/MarkdownTextBlock/MarkdownTextBlock.Methods.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Markdown/MarkdownTextBlock/MarkdownTextBlock.Methods.cs index 57734e744ce..ecd31928824 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Markdown/MarkdownTextBlock/MarkdownTextBlock.Methods.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Markdown/MarkdownTextBlock/MarkdownTextBlock.Methods.cs @@ -10,7 +10,7 @@ using ColorCode; using Microsoft.Toolkit.Parsers.Markdown; using Microsoft.Toolkit.Uwp.UI.Controls.Markdown.Render; -using Windows.UI.Core; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Documents; @@ -347,7 +347,8 @@ internal async void LinkHandled(string url, bool isHyperlink) } multiClickDetectionTriggered = true; - await Dispatcher.RunAsync(CoreDispatcherPriority.High, () => multiClickDetectionTriggered = false); + var dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + await dispatcherQueue.EnqueueAsync(() => multiClickDetectionTriggered = false, DispatcherQueuePriority.High); // Get the hyperlink URL. if (url == null) diff --git a/Microsoft.Toolkit.Uwp.UI.Media/Brushes/XamlCompositionBrush.cs b/Microsoft.Toolkit.Uwp.UI.Media/Brushes/XamlCompositionBrush.cs index 220a0261b77..095607858cc 100644 --- a/Microsoft.Toolkit.Uwp.UI.Media/Brushes/XamlCompositionBrush.cs +++ b/Microsoft.Toolkit.Uwp.UI.Media/Brushes/XamlCompositionBrush.cs @@ -6,6 +6,7 @@ using System.Diagnostics.Contracts; using System.Threading.Tasks; using Microsoft.Toolkit.Uwp.UI.Media.Pipelines; +using Windows.System; namespace Microsoft.Toolkit.Uwp.UI.Media { @@ -79,20 +80,16 @@ public XamlCompositionBrush Bind(EffectAnimation animation, out XamlEffect protected override PipelineBuilder OnPipelineRequested() => this.Pipeline; /// - /// Clones the current instance by rebuilding the source . Use this method to reuse the same effects pipeline on a different + /// Clones the current instance by rebuilding the source . Use this method to reuse the same effects pipeline on a different /// + /// + /// If your code is already on the same thread, you can just assign this brush to an arbitrary number of controls and it will still work correctly. + /// This method is only meant to be used to create a new instance of this brush using the same pipeline, on threads that can't access the current instance, for example in secondary app windows. + /// /// A instance using the current effects pipeline [Pure] public XamlCompositionBrush Clone() { - if (this.Dispatcher.HasThreadAccess) - { - throw new InvalidOperationException("The current thread already has access to the brush dispatcher, so a clone operation is not necessary. " + - "You can just assign this brush to an arbitrary number of controls and it will still work correctly. " + - "This method is only meant to be used to create a new instance of this brush using the same pipeline, " + - "on threads that can't access the current instance, for example in secondary app windows."); - } - return new XamlCompositionBrush(this.Pipeline); } } diff --git a/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs b/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs index aabf5ba9531..227c4fc8674 100644 --- a/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs +++ b/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs @@ -4,6 +4,7 @@ using System; using Windows.Networking.Connectivity; +using Windows.System; using Windows.UI.Xaml; namespace Microsoft.Toolkit.Uwp.UI.Triggers @@ -13,11 +14,14 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers /// public class NetworkConnectionStateTrigger : StateTriggerBase { + private DispatcherQueue _dispatcherQueue; + /// /// Initializes a new instance of the class. /// public NetworkConnectionStateTrigger() { + _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); var weakEvent = new WeakEventListener(this) { @@ -30,7 +34,7 @@ public NetworkConnectionStateTrigger() private void NetworkInformation_NetworkStatusChanged(object sender) { - _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, UpdateState); + _ = _dispatcherQueue.EnqueueAsync(UpdateState, DispatcherQueuePriority.Normal); } private void UpdateState() From c2e3c526d192306b49552a4c652fe35a9287a719 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Wed, 10 Mar 2021 12:42:36 -0800 Subject: [PATCH 2/2] Changed all DispatcherTimer to DispatcherQueueTimer. --- .../FocusTracker/FocusTracker.cs | 5 ++-- .../GazePointer.cs | 5 ++-- .../Focus/FocusBehavior.cs | 9 +++---- .../RotatorTile/RotatorTile.cs | 6 +++-- .../TileControl/TileControl.cs | 5 ++-- .../DataGrid/DataGrid.cs | 12 ++++----- .../ColorPicker/ColorPicker.cs | 27 +++++++++---------- 7 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.DeveloperTools/FocusTracker/FocusTracker.cs b/Microsoft.Toolkit.Uwp.DeveloperTools/FocusTracker/FocusTracker.cs index 8cc04f09ecd..10d6f3f22de 100644 --- a/Microsoft.Toolkit.Uwp.DeveloperTools/FocusTracker/FocusTracker.cs +++ b/Microsoft.Toolkit.Uwp.DeveloperTools/FocusTracker/FocusTracker.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Automation; using Windows.UI.Xaml.Controls; @@ -45,7 +46,7 @@ private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChan } } - private DispatcherTimer updateTimer; + private DispatcherQueueTimer updateTimer; private TextBlock controlName; private TextBlock controlType; private TextBlock controlAutomationName; @@ -72,7 +73,7 @@ private void Start() { if (updateTimer == null) { - updateTimer = new DispatcherTimer(); + updateTimer = DispatcherQueue.GetForCurrentThread().CreateTimer(); updateTimer.Tick += UpdateTimer_Tick; } diff --git a/Microsoft.Toolkit.Uwp.Input.GazeInteraction/GazePointer.cs b/Microsoft.Toolkit.Uwp.Input.GazeInteraction/GazePointer.cs index 5228c7650db..031d760ebb5 100644 --- a/Microsoft.Toolkit.Uwp.Input.GazeInteraction/GazePointer.cs +++ b/Microsoft.Toolkit.Uwp.Input.GazeInteraction/GazePointer.cs @@ -11,6 +11,7 @@ using Windows.Devices.Input.Preview; using Windows.Foundation; using Windows.Foundation.Collections; +using Windows.System; using Windows.UI; using Windows.UI.Core; using Windows.UI.Xaml; @@ -354,7 +355,7 @@ private GazePointer() _gazeCursor = new GazeCursor(); // timer that gets called back if there gaze samples haven't been received in a while - _eyesOffTimer = new DispatcherTimer(); + _eyesOffTimer = DispatcherQueue.GetForCurrentThread().CreateTimer(); _eyesOffTimer.Tick += OnEyesOff; // provide a default of GAZE_IDLE_TIME microseconds to fire eyes off @@ -860,7 +861,7 @@ private void OnDeviceRemoved(GazeDeviceWatcherPreview sender, GazeDeviceWatcherR private readonly List _roots = new List(); - private readonly DispatcherTimer _eyesOffTimer; + private readonly DispatcherQueueTimer _eyesOffTimer; private readonly GazeCursor _gazeCursor; diff --git a/Microsoft.Toolkit.Uwp.UI.Behaviors/Focus/FocusBehavior.cs b/Microsoft.Toolkit.Uwp.UI.Behaviors/Focus/FocusBehavior.cs index 8c1e3a84fba..17db56da05b 100644 --- a/Microsoft.Toolkit.Uwp.UI.Behaviors/Focus/FocusBehavior.cs +++ b/Microsoft.Toolkit.Uwp.UI.Behaviors/Focus/FocusBehavior.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Markup; @@ -39,7 +40,7 @@ public sealed class FocusBehavior : BehaviorBase typeof(FocusBehavior), new PropertyMetadata(TimeSpan.FromMilliseconds(100))); - private DispatcherTimer _timer; + private DispatcherQueueTimer _timer; /// /// Initializes a new instance of the class. @@ -120,10 +121,8 @@ private void ApplyFocus() // This allows us to handle the case where the controls are not loaded in the order we expect. if (_timer is null) { - _timer = new DispatcherTimer - { - Interval = FocusEngagementTimeout, - }; + _timer = DispatcherQueue.GetForCurrentThread().CreateTimer(); + _timer.Interval = FocusEngagementTimeout; _timer.Tick += OnEngagementTimerTick; _timer.Start(); } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Core/RotatorTile/RotatorTile.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Core/RotatorTile/RotatorTile.cs index 804cbc0195e..89837196596 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Core/RotatorTile/RotatorTile.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Core/RotatorTile/RotatorTile.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Specialized; using Microsoft.Toolkit.Uwp.Helpers; +using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; @@ -32,7 +33,7 @@ public partial class RotatorTile : Control private static readonly Random Randomizer = new Random(); private int _currentIndex = -1; // current index in the items displayed - private DispatcherTimer _timer; // timer for triggering when to flip the content + private DispatcherQueueTimer _timer; // timer for triggering when to flip the content private FrameworkElement _currentElement; // FrameworkElement holding a reference to the current element being display private FrameworkElement _nextElement; // FrameworkElement holding a reference to the next element being display private FrameworkElement _scroller; // Container Element that's being translated to animate from one item to the next @@ -387,7 +388,8 @@ private void Start() if (_timer == null) { - _timer = new DispatcherTimer() { Interval = GetTileDuration() }; + _timer = DispatcherQueue.GetForCurrentThread().CreateTimer(); + _timer.Interval = GetTileDuration(); _timer.Tick += Timer_Tick; } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Core/TileControl/TileControl.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Core/TileControl/TileControl.cs index e6337603864..20df3bf2c04 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Core/TileControl/TileControl.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Core/TileControl/TileControl.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Windows.Foundation; +using Windows.System; using Windows.UI.Composition; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -39,7 +40,7 @@ public partial class TileControl : ContentControl private Size _imageSize = Size.Empty; - private DispatcherTimer _timerAnimation; + private DispatcherQueueTimer _timerAnimation; /// /// A ScrollViewer used for synchronized the move of the @@ -609,7 +610,7 @@ private void InitializeAnimation() { if (_timerAnimation == null) { - _timerAnimation = new DispatcherTimer(); + _timerAnimation = DispatcherQueue.GetForCurrentThread().CreateTimer(); } else { diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs index 6d1423d5202..561e69e4182 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs @@ -171,7 +171,7 @@ private enum ScrollBarsSeparatorVisualState private DataGridRow _focusedRow; private FrameworkElement _frozenColumnScrollBarSpacer; private bool _hasNoIndicatorStateStoryboardCompletedHandler; - private DispatcherTimer _hideScrollBarsTimer; + private DispatcherQueueTimer _hideScrollBarsTimer; // the sum of the widths in pixels of the scrolling columns preceding // the first displayed scrolling column @@ -6423,19 +6423,19 @@ private void HideScrollBarsAfterDelay() { if (!_keepScrollBarsShowing) { - DispatcherTimer hideScrollBarsTimer = null; + DispatcherQueueTimer hideScrollBarsTimer = null; if (_hideScrollBarsTimer != null) { hideScrollBarsTimer = _hideScrollBarsTimer; - if (hideScrollBarsTimer.IsEnabled) + if (hideScrollBarsTimer.IsRunning) { hideScrollBarsTimer.Stop(); } } else { - hideScrollBarsTimer = new DispatcherTimer(); + hideScrollBarsTimer = DispatcherQueue.GetForCurrentThread().CreateTimer(); hideScrollBarsTimer.Interval = TimeSpan.FromMilliseconds(DATAGRID_noScrollBarCountdownMs); hideScrollBarsTimer.Tick += HideScrollBarsTimerTick; _hideScrollBarsTimer = hideScrollBarsTimer; @@ -8208,7 +8208,7 @@ private void ShowScrollBars() } else { - if (_hideScrollBarsTimer != null && _hideScrollBarsTimer.IsEnabled) + if (_hideScrollBarsTimer != null && _hideScrollBarsTimer.IsRunning) { _hideScrollBarsTimer.Stop(); _hideScrollBarsTimer.Start(); @@ -8290,7 +8290,7 @@ private void ShowScrollBars() private void StopHideScrollBarsTimer() { - if (_hideScrollBarsTimer != null && _hideScrollBarsTimer.IsEnabled) + if (_hideScrollBarsTimer != null && _hideScrollBarsTimer.IsRunning) { _hideScrollBarsTimer.Stop(); } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.cs index 8dbe534bf94..98ed480a8c7 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.cs @@ -8,6 +8,7 @@ using System.Globalization; using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.UI.Controls.ColorPickerConverters; +using Windows.System; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -74,7 +75,7 @@ public partial class ColorPicker : Windows.UI.Xaml.Controls.ColorPicker private HsvColor? savedHsvColor = null; private Color? savedHsvColorRgbEquivalent = null; private Color? updatedRgbColor = null; - private DispatcherTimer dispatcherTimer = null; + private DispatcherQueueTimer dispatcherQueueTimer = null; private ColorSpectrum ColorSpectrumControl; private ColorPickerSlider ColorSpectrumAlphaSlider; @@ -134,7 +135,7 @@ public ColorPicker() this.ConnectCallbacks(true); this.SetDefaultPalette(); - this.StartDispatcherTimer(); + this.StartDispatcherQueueTimer(); } /// @@ -142,7 +143,7 @@ public ColorPicker() /// ~ColorPicker() { - this.StopDispatcherTimer(); + this.StopDispatcherQueueTimer(); this.CustomPaletteColors.CollectionChanged -= CustomPaletteColors_CollectionChanged; } @@ -1068,29 +1069,27 @@ private void SetDefaultPalette() * ***************************************************************************************/ - private void StartDispatcherTimer() + private void StartDispatcherQueueTimer() { - this.dispatcherTimer = new DispatcherTimer() - { - Interval = new TimeSpan(0, 0, 0, 0, ColorUpdateInterval) - }; - this.dispatcherTimer.Tick += DispatcherTimer_Tick; - this.dispatcherTimer.Start(); + this.dispatcherQueueTimer = DispatcherQueue.GetForCurrentThread().CreateTimer(); + this.dispatcherQueueTimer.Interval = new TimeSpan(0, 0, 0, 0, ColorUpdateInterval); + this.dispatcherQueueTimer.Tick += DispatcherQueueTimer_Tick; + this.dispatcherQueueTimer.Start(); return; } - private void StopDispatcherTimer() + private void StopDispatcherQueueTimer() { - if (this.dispatcherTimer != null) + if (this.dispatcherQueueTimer != null) { - this.dispatcherTimer.Stop(); + this.dispatcherQueueTimer.Stop(); } return; } - private void DispatcherTimer_Tick(object sender, object e) + private void DispatcherQueueTimer_Tick(object sender, object e) { if (this.updatedRgbColor != null) {