Skip to content

Commit 2352a46

Browse files
committed
Moved a few things to use the DispatcherQueue.
1 parent 8829b82 commit 2352a46

File tree

8 files changed

+93
-79
lines changed

8 files changed

+93
-79
lines changed

Microsoft.Toolkit.Uwp.UI.Animations/ConnectedAnimations/ConnectedAnimationHelper.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using Windows.System;
89
using Windows.UI.Xaml;
910
using Windows.UI.Xaml.Controls;
1011
using Windows.UI.Xaml.Media.Animation;
@@ -149,17 +150,19 @@ void LoadedHandler(object s, RoutedEventArgs args)
149150
listAnimProperty.ListViewBase.ScrollIntoView(parameter);
150151

151152
// give time to the UI thread to scroll the list
152-
var t = listAnimProperty.ListViewBase.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
153-
{
154-
try
153+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
154+
var t = dispatcherQueue.EnqueueAsync(
155+
async () =>
155156
{
156-
var success = await listAnimProperty.ListViewBase.TryStartConnectedAnimationAsync(connectedAnimation, parameter, listAnimProperty.ElementName);
157-
}
158-
catch (Exception)
159-
{
160-
connectedAnimation.Cancel();
161-
}
162-
});
157+
try
158+
{
159+
var success = await listAnimProperty.ListViewBase.TryStartConnectedAnimationAsync(connectedAnimation, parameter, listAnimProperty.ElementName);
160+
}
161+
catch (Exception)
162+
{
163+
connectedAnimation.Cancel();
164+
}
165+
}, DispatcherQueuePriority.Normal);
163166

164167
animationHandled = true;
165168
}

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7958,6 +7958,7 @@ private void ResetValidationStatus()
79587958
int editingRowSlot = this.EditingRow.Slot;
79597959

79607960
InvalidateMeasure();
7961+
// TODO: Move to DispatcherQueue when FEATURE_VALIDATION_SUMMARY is enabled
79617962
this.Dispatcher.BeginInvoke(() =>
79627963
{
79637964
// 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<ValidationResult> newValidationResults
87628763
// If the number of errors has changed, then the ValidationSummary will be a different size,
87638764
// and we need to delay our call to ScrollSlotIntoView
87648765
this.InvalidateMeasure();
8766+
// TODO: Move to DispatcherQueue when FEATURE_VALIDATION_SUMMARY is enabled
87658767
this.Dispatcher.BeginInvoke(() =>
87668768
{
87678769
// It's possible that the DataContext or ItemsSource has changed by the time we reach this code,

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.Selection.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System;
66
using System.Threading.Tasks;
77
using Windows.ApplicationModel.DataTransfer;
8-
using Windows.UI.Core;
8+
using Windows.System;
99
using Windows.UI.Xaml;
1010
using Windows.UI.Xaml.Controls;
1111
using Windows.UI.Xaml.Input;
@@ -136,28 +136,30 @@ private TokenizingTextBoxItem GetCurrentContainerItem()
136136

137137
internal void SelectAllTokensAndText()
138138
{
139-
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
140-
{
141-
this.SelectAllSafe();
139+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
140+
_ = dispatcherQueue.EnqueueAsync(
141+
() =>
142+
{
143+
this.SelectAllSafe();
142144

143-
// need to synchronize the select all and the focus behavior on the text box
144-
// because there is no way to identify that the focus has been set from this point
145-
// to avoid instantly clearing the selection of tokens
146-
PauseTokenClearOnFocus = true;
145+
// need to synchronize the select all and the focus behavior on the text box
146+
// because there is no way to identify that the focus has been set from this point
147+
// to avoid instantly clearing the selection of tokens
148+
PauseTokenClearOnFocus = true;
147149

148-
foreach (var item in Items)
149-
{
150-
if (item is ITokenStringContainer)
150+
foreach (var item in Items)
151151
{
152-
// grab any selected text
153-
var pretoken = ContainerFromItem(item) as TokenizingTextBoxItem;
154-
pretoken._autoSuggestTextBox.SelectionStart = 0;
155-
pretoken._autoSuggestTextBox.SelectionLength = pretoken._autoSuggestTextBox.Text.Length;
152+
if (item is ITokenStringContainer)
153+
{
154+
// grab any selected text
155+
var pretoken = ContainerFromItem(item) as TokenizingTextBoxItem;
156+
pretoken._autoSuggestTextBox.SelectionStart = 0;
157+
pretoken._autoSuggestTextBox.SelectionLength = pretoken._autoSuggestTextBox.Text.Length;
158+
}
156159
}
157-
}
158160

159-
(ContainerFromIndex(Items.Count - 1) as TokenizingTextBoxItem).Focus(FocusState.Programmatic);
160-
});
161+
(ContainerFromIndex(Items.Count - 1) as TokenizingTextBoxItem).Focus(FocusState.Programmatic);
162+
}, DispatcherQueuePriority.Normal);
161163
}
162164

163165
internal void DeselectAllTokensAndText(TokenizingTextBoxItem ignoreItem = null)

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -224,54 +224,57 @@ private async void TokenizingTextBox_CharacterReceived(UIElement sender, Charact
224224
await RemoveAllSelectedTokens();
225225

226226
// Wait for removal of old items
227-
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
228-
{
229-
// If we're before the last textbox and it's empty, redirect focus to that one instead
230-
if (index == _innerItemsSource.Count - 1 && string.IsNullOrWhiteSpace(_lastTextEdit.Text))
227+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
228+
_ = dispatcherQueue.EnqueueAsync(
229+
() =>
231230
{
232-
var lastContainer = ContainerFromItem(_lastTextEdit) as TokenizingTextBoxItem;
231+
// If we're before the last textbox and it's empty, redirect focus to that one instead
232+
if (index == _innerItemsSource.Count - 1 && string.IsNullOrWhiteSpace(_lastTextEdit.Text))
233+
{
234+
var lastContainer = ContainerFromItem(_lastTextEdit) as TokenizingTextBoxItem;
233235

234-
lastContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items.
236+
lastContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items.
235237

236-
_lastTextEdit.Text = string.Empty + args.Character;
238+
_lastTextEdit.Text = string.Empty + args.Character;
237239

238-
UpdateCurrentTextEdit(_lastTextEdit);
240+
UpdateCurrentTextEdit(_lastTextEdit);
239241

240-
lastContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted
242+
lastContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted
241243

242-
lastContainer._autoSuggestTextBox.Focus(FocusState.Keyboard);
243-
}
244-
else
245-
{
246-
//// Otherwise, create a new textbox for this text.
244+
lastContainer._autoSuggestTextBox.Focus(FocusState.Keyboard);
245+
}
246+
else
247+
{
248+
//// Otherwise, create a new textbox for this text.
247249

248-
UpdateCurrentTextEdit(new PretokenStringContainer((string.Empty + args.Character).Trim())); // Trim so that 'space' isn't inserted and can be used to insert a new box.
250+
UpdateCurrentTextEdit(new PretokenStringContainer((string.Empty + args.Character).Trim())); // Trim so that 'space' isn't inserted and can be used to insert a new box.
249251

250-
_innerItemsSource.Insert(index, _currentTextEdit);
252+
_innerItemsSource.Insert(index, _currentTextEdit);
251253

252-
// Need to wait for containerization
253-
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
254-
{
255-
var newContainer = ContainerFromIndex(index) as TokenizingTextBoxItem; // Should be our last text box
254+
// Need to wait for containerization
255+
_ = dispatcherQueue.EnqueueAsync(
256+
() =>
257+
{
258+
var newContainer = ContainerFromIndex(index) as TokenizingTextBoxItem; // Should be our last text box
256259

257-
newContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items.
260+
newContainer.UseCharacterAsUser = true; // Make sure we trigger a refresh of suggested items.
258261

259-
void WaitForLoad(object s, RoutedEventArgs eargs)
260-
{
261-
if (newContainer._autoSuggestTextBox != null)
262-
{
263-
newContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted
262+
void WaitForLoad(object s, RoutedEventArgs eargs)
263+
{
264+
if (newContainer._autoSuggestTextBox != null)
265+
{
266+
newContainer._autoSuggestTextBox.SelectionStart = 1; // Set position to after our new character inserted
264267

265-
newContainer._autoSuggestTextBox.Focus(FocusState.Keyboard);
266-
}
268+
newContainer._autoSuggestTextBox.Focus(FocusState.Keyboard);
269+
}
267270

268-
newContainer.Loaded -= WaitForLoad;
269-
}
271+
newContainer.Loaded -= WaitForLoad;
272+
}
270273

271-
newContainer.AutoSuggestTextBoxLoaded += WaitForLoad;
272-
});
273-
}
274-
});
274+
newContainer.AutoSuggestTextBoxLoaded += WaitForLoad;
275+
}, DispatcherQueuePriority.Normal);
276+
}
277+
}, DispatcherQueuePriority.Normal);
275278
}
276279
else
277280
{

Microsoft.Toolkit.Uwp.UI.Controls.Layout/BladeView/BladeView.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
1010
using Windows.Foundation;
1111
using Windows.Foundation.Collections;
12-
using Windows.UI.Core;
12+
using Windows.System;
1313
using Windows.UI.Xaml;
1414
using Windows.UI.Xaml.Automation.Peers;
1515
using Windows.UI.Xaml.Controls;
@@ -153,10 +153,12 @@ private async void BladeOnVisibilityChanged(object sender, Visibility visibility
153153
UpdateLayout();
154154

155155
// Need to do this because of touch. See more information here: https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/760#issuecomment-276466464
156-
await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
157-
{
158-
GetScrollViewer()?.ChangeView(_scrollViewer.ScrollableWidth, null, null);
159-
});
156+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
157+
await dispatcherQueue.EnqueueAsync(
158+
() =>
159+
{
160+
GetScrollViewer()?.ChangeView(_scrollViewer.ScrollableWidth, null, null);
161+
}, DispatcherQueuePriority.Low);
160162

161163
return;
162164
}

Microsoft.Toolkit.Uwp.UI.Controls.Markdown/MarkdownTextBlock/MarkdownTextBlock.Methods.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using ColorCode;
1111
using Microsoft.Toolkit.Parsers.Markdown;
1212
using Microsoft.Toolkit.Uwp.UI.Controls.Markdown.Render;
13-
using Windows.UI.Core;
13+
using Windows.System;
1414
using Windows.UI.Xaml;
1515
using Windows.UI.Xaml.Controls;
1616
using Windows.UI.Xaml.Documents;
@@ -347,7 +347,8 @@ internal async void LinkHandled(string url, bool isHyperlink)
347347
}
348348

349349
multiClickDetectionTriggered = true;
350-
await Dispatcher.RunAsync(CoreDispatcherPriority.High, () => multiClickDetectionTriggered = false);
350+
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
351+
await dispatcherQueue.EnqueueAsync(() => multiClickDetectionTriggered = false, DispatcherQueuePriority.High);
351352

352353
// Get the hyperlink URL.
353354
if (url == null)

Microsoft.Toolkit.Uwp.UI.Media/Brushes/XamlCompositionBrush.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics.Contracts;
77
using System.Threading.Tasks;
88
using Microsoft.Toolkit.Uwp.UI.Media.Pipelines;
9+
using Windows.System;
910

1011
namespace Microsoft.Toolkit.Uwp.UI.Media
1112
{
@@ -79,20 +80,16 @@ public XamlCompositionBrush Bind<T>(EffectAnimation<T> animation, out XamlEffect
7980
protected override PipelineBuilder OnPipelineRequested() => this.Pipeline;
8081

8182
/// <summary>
82-
/// Clones the current instance by rebuilding the source <see cref="Windows.UI.Xaml.Media.Brush"/>. Use this method to reuse the same effects pipeline on a different <see cref="Windows.UI.Core.CoreDispatcher"/>
83+
/// Clones the current instance by rebuilding the source <see cref="Windows.UI.Xaml.Media.Brush"/>. Use this method to reuse the same effects pipeline on a different <see cref="DispatcherQueue"/>
8384
/// </summary>
85+
/// <remarks>
86+
/// 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.
87+
/// 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.
88+
/// </remarks>
8489
/// <returns>A <see cref="XamlCompositionBrush"/> instance using the current effects pipeline</returns>
8590
[Pure]
8691
public XamlCompositionBrush Clone()
8792
{
88-
if (this.Dispatcher.HasThreadAccess)
89-
{
90-
throw new InvalidOperationException("The current thread already has access to the brush dispatcher, so a clone operation is not necessary. " +
91-
"You can just assign this brush to an arbitrary number of controls and it will still work correctly. " +
92-
"This method is only meant to be used to create a new instance of this brush using the same pipeline, " +
93-
"on threads that can't access the current instance, for example in secondary app windows.");
94-
}
95-
9693
return new XamlCompositionBrush(this.Pipeline);
9794
}
9895
}

Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using Windows.Networking.Connectivity;
7+
using Windows.System;
78
using Windows.UI.Xaml;
89

910
namespace Microsoft.Toolkit.Uwp.UI.Triggers
@@ -13,11 +14,14 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers
1314
/// </summary>
1415
public class NetworkConnectionStateTrigger : StateTriggerBase
1516
{
17+
private DispatcherQueue _dispatcherQueue;
18+
1619
/// <summary>
1720
/// Initializes a new instance of the <see cref="NetworkConnectionStateTrigger"/> class.
1821
/// </summary>
1922
public NetworkConnectionStateTrigger()
2023
{
24+
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
2125
var weakEvent =
2226
new WeakEventListener<NetworkConnectionStateTrigger, object>(this)
2327
{
@@ -30,7 +34,7 @@ public NetworkConnectionStateTrigger()
3034

3135
private void NetworkInformation_NetworkStatusChanged(object sender)
3236
{
33-
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, UpdateState);
37+
_ = _dispatcherQueue.EnqueueAsync(UpdateState, DispatcherQueuePriority.Normal);
3438
}
3539

3640
private void UpdateState()

0 commit comments

Comments
 (0)