Skip to content

Commit 2a4938d

Browse files
Merge pull request #3596 from Sergio0694/bugfix/dispatch-refactoring
Removed leftover DispatcherQueue usages
2 parents 54c7c8c + 8f3738d commit 2a4938d

File tree

8 files changed

+36
-32
lines changed

8 files changed

+36
-32
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperPage.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Threading.Tasks;
6-
using Microsoft.Toolkit.Uwp.Helpers;
6+
using Microsoft.Toolkit.Uwp.Extensions;
77
using Windows.System;
88
using Windows.UI.Xaml;
9-
using Microsoft.Toolkit.Uwp.Extensions;
109

1110
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
1211
{

Microsoft.Toolkit.Uwp.SampleApp/Shell.SamplePicker.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Numerics;
99
using System.Threading.Tasks;
10+
using Microsoft.Toolkit.Uwp.Extensions;
1011
using Microsoft.Toolkit.Uwp.Helpers;
1112
using Microsoft.Toolkit.Uwp.SampleApp.Pages;
1213
using Microsoft.Toolkit.Uwp.UI.Animations;
@@ -147,7 +148,7 @@ private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sende
147148
ShowSamplePicker(category.Samples, true);
148149

149150
// Then Focus on Picker
150-
DispatcherHelper.ExecuteOnUIThreadAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
151+
dispatcherQueue.EnqueueAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
151152
}
152153
}
153154
else if (args.IsSettingsInvoked)

Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Linq;
6+
using Microsoft.Toolkit.Uwp.Extensions;
67
using Microsoft.Toolkit.Uwp.Helpers;
78
using Microsoft.Toolkit.Uwp.UI.Extensions;
89
using Windows.UI.Xaml;
@@ -76,7 +77,7 @@ private void SearchBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEve
7677
if (e.Key == Windows.System.VirtualKey.Down && SamplePickerGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
7778
{
7879
// If we try and navigate down out of the textbox (and there's search results), go to the search results.
79-
DispatcherHelper.ExecuteOnUIThreadAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
80+
dispatcherQueue.EnqueueAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
8081
}
8182
}
8283

Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.Toolkit.Uwp.SampleApp.Pages;
88
using Microsoft.Toolkit.Uwp.UI.Extensions;
99
using Windows.System;
10-
using Windows.UI.Xaml;
1110
using Windows.UI.Xaml.Controls;
1211
using Windows.UI.Xaml.Media.Animation;
1312
using Windows.UI.Xaml.Navigation;
@@ -16,6 +15,8 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
1615
{
1716
public sealed partial class Shell
1817
{
18+
private readonly DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();
19+
1920
public static Shell Current { get; private set; }
2021

2122
public Shell()

Microsoft.Toolkit.Uwp.UI.Animations/Extensions/AnimationExtensions.Light.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Collections.Generic;
77
using Microsoft.Graphics.Canvas;
88
using Microsoft.Graphics.Canvas.Effects;
9-
using Microsoft.Toolkit.Uwp.Helpers;
9+
using Microsoft.Toolkit.Uwp.Extensions;
1010
using Windows.UI;
1111
using Windows.UI.Composition;
1212
using Windows.UI.Composition.Effects;
@@ -102,7 +102,7 @@ public static AnimationSet Light(
102102
var task = new AnimationTask();
103103
task.AnimationSet = animationSet;
104104

105-
task.Task = DispatcherHelper.ExecuteOnUIThreadAsync(
105+
task.Task = visual.DispatcherQueue.EnqueueAsync(
106106
() =>
107107
{
108108
const string sceneName = "PointLightScene";
@@ -184,7 +184,7 @@ public static AnimationSet Light(
184184
}
185185

186186
pointLights[visual] = pointLight;
187-
}, Windows.UI.Core.CoreDispatcherPriority.Normal);
187+
});
188188

189189
animationSet.AddAnimationThroughTask(task);
190190
return animationSet;

Microsoft.Toolkit.Uwp.UI/Extensions/ScrollViewer/ScrollViewerExtensions.MiddleClickScrolling.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ private static void Scroll(object state)
162162
offsetX = offsetX > _maxSpeed ? _maxSpeed : offsetX;
163163
offsetY = offsetY > _maxSpeed ? _maxSpeed : offsetY;
164164

165-
RunInUIThread(dispatcherQueue, () =>
166-
{
167-
_scrollViewer?.ChangeView(_scrollViewer.HorizontalOffset + offsetX, _scrollViewer.VerticalOffset + offsetY, null, true);
168-
});
165+
dispatcherQueue.EnqueueAsync(() => _scrollViewer?.ChangeView(_scrollViewer.HorizontalOffset + offsetX, _scrollViewer.VerticalOffset + offsetY, null, true));
169166
}
170167
}
171168

@@ -326,10 +323,7 @@ private static void SetCursorType(DispatcherQueue dispatcherQueue, double offset
326323

327324
if (_oldCursorID != cursorID)
328325
{
329-
RunInUIThread(dispatcherQueue, () =>
330-
{
331-
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Custom, cursorID);
332-
});
326+
dispatcherQueue.EnqueueAsync(() => Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Custom, cursorID));
333327

334328
_oldCursorID = cursorID;
335329
}
@@ -366,10 +360,5 @@ private static bool IsCursorResourceAvailable()
366360

367361
return isCursorAvailable;
368362
}
369-
370-
private static async void RunInUIThread(DispatcherQueue dispatcherQueue, Action action)
371-
{
372-
await dispatcherQueue.EnqueueAsync(action, DispatcherQueuePriority.Normal);
373-
}
374363
}
375364
}

Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Runtime.CompilerServices;
67
using System.Threading.Tasks;
78
using Windows.Foundation.Metadata;
89
using Windows.System;
@@ -67,7 +68,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Action function, Dispatc
6768
}
6869
}))
6970
{
70-
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
71+
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
7172
}
7273

7374
return taskCompletionSource.Task;
@@ -116,7 +117,7 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<T> function, Dis
116117
}
117118
}))
118119
{
119-
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
120+
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
120121
}
121122

122123
return taskCompletionSource.Task;
@@ -149,7 +150,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func<Task> func
149150
return awaitableResult;
150151
}
151152

152-
return Task.FromException(new InvalidOperationException("The Task returned by function cannot be null."));
153+
return Task.FromException(GetEnqueueException("The Task returned by function cannot be null."));
153154
}
154155
catch (Exception e)
155156
{
@@ -173,7 +174,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
173174
}
174175
else
175176
{
176-
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
177+
taskCompletionSource.SetException(GetEnqueueException("The Task returned by function cannot be null."));
177178
}
178179
}
179180
catch (Exception e)
@@ -182,7 +183,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
182183
}
183184
}))
184185
{
185-
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
186+
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
186187
}
187188

188189
return taskCompletionSource.Task;
@@ -212,7 +213,7 @@ public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<Task
212213
return awaitableResult;
213214
}
214215

215-
return Task.FromException<T>(new InvalidOperationException("The Task returned by function cannot be null."));
216+
return Task.FromException<T>(GetEnqueueException("The Task returned by function cannot be null."));
216217
}
217218
catch (Exception e)
218219
{
@@ -236,7 +237,7 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
236237
}
237238
else
238239
{
239-
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
240+
taskCompletionSource.SetException(GetEnqueueException("The Task returned by function cannot be null."));
240241
}
241242
}
242243
catch (Exception e)
@@ -245,13 +246,24 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
245246
}
246247
}))
247248
{
248-
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
249+
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
249250
}
250251

251252
return taskCompletionSource.Task;
252253
}
253254

254255
return TryEnqueueAsync(dispatcher, function, priority);
255256
}
257+
258+
/// <summary>
259+
/// Creates an <see cref="InvalidOperationException"/> to return when an enqueue operation fails.
260+
/// </summary>
261+
/// <param name="message">The message of the exception.</param>
262+
/// <returns>An <see cref="InvalidOperationException"/> with a specified message.</returns>
263+
[MethodImpl(MethodImplOptions.NoInlining)]
264+
private static InvalidOperationException GetEnqueueException(string message)
265+
{
266+
return new InvalidOperationException(message);
267+
}
256268
}
257269
}

UnitTests/UnitTests.XamlIslands.UWPApp/XamlIslandsTest_Gaze.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Linq;
66
using System.Threading.Tasks;
7+
using Microsoft.Toolkit.Uwp.Extensions;
78
using Microsoft.Toolkit.Uwp.Helpers;
89
using Microsoft.Toolkit.Uwp.Input.GazeInteraction;
910
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -21,7 +22,7 @@ public partial class XamlIslandsTest_Gaze
2122
[TestInitialize]
2223
public async Task Init()
2324
{
24-
await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
25+
await App.Dispatcher.EnqueueAsync(() =>
2526
{
2627
var xamlItemsPanelTemplate = @"<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
2728
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
@@ -65,7 +66,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
6566
[TestCleanup]
6667
public async Task Cleanup()
6768
{
68-
await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
69+
await App.Dispatcher.EnqueueAsync(() =>
6970
{
7071
GazeInput.SetInteraction(_grid, Interaction.Disabled);
7172
});
@@ -76,7 +77,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
7677
[Ignore]
7778
public async Task Gaze_DoesNotCrashOnIslands()
7879
{
79-
await App.Dispatcher.ExecuteOnUIThreadAsync(async () =>
80+
await App.Dispatcher.EnqueueAsync(async () =>
8081
{
8182
await Task.Delay(10000);
8283

0 commit comments

Comments
 (0)