Skip to content

Removed leftover DispatcherQueue usages #3596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.System;
using Windows.UI.Xaml;
using Microsoft.Toolkit.Uwp.Extensions;

namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Shell.SamplePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.SampleApp.Pages;
using Microsoft.Toolkit.Uwp.UI.Animations;
Expand Down Expand Up @@ -147,7 +148,7 @@ private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sende
ShowSamplePicker(category.Samples, true);

// Then Focus on Picker
DispatcherHelper.ExecuteOnUIThreadAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
dispatcherQueue.EnqueueAsync(() => SamplePickerGridView.Focus(FocusState.Keyboard));
}
}
else if (args.IsSettingsInvoked)
Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

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

Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Shell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Toolkit.Uwp.SampleApp.Pages;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
Expand All @@ -16,6 +15,8 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
{
public sealed partial class Shell
{
private readonly DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();

public static Shell Current { get; private set; }

public Shell()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Collections.Generic;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Composition.Effects;
Expand Down Expand Up @@ -102,7 +102,7 @@ public static AnimationSet Light(
var task = new AnimationTask();
task.AnimationSet = animationSet;

task.Task = DispatcherHelper.ExecuteOnUIThreadAsync(
task.Task = visual.DispatcherQueue.EnqueueAsync(
() =>
{
const string sceneName = "PointLightScene";
Expand Down Expand Up @@ -184,7 +184,7 @@ public static AnimationSet Light(
}

pointLights[visual] = pointLight;
}, Windows.UI.Core.CoreDispatcherPriority.Normal);
});

animationSet.AddAnimationThroughTask(task);
return animationSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ private static void Scroll(object state)
offsetX = offsetX > _maxSpeed ? _maxSpeed : offsetX;
offsetY = offsetY > _maxSpeed ? _maxSpeed : offsetY;

RunInUIThread(dispatcherQueue, () =>
{
_scrollViewer?.ChangeView(_scrollViewer.HorizontalOffset + offsetX, _scrollViewer.VerticalOffset + offsetY, null, true);
});
dispatcherQueue.EnqueueAsync(() => _scrollViewer?.ChangeView(_scrollViewer.HorizontalOffset + offsetX, _scrollViewer.VerticalOffset + offsetY, null, true));
}
}

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

if (_oldCursorID != cursorID)
{
RunInUIThread(dispatcherQueue, () =>
{
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Custom, cursorID);
});
dispatcherQueue.EnqueueAsync(() => Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Custom, cursorID));

_oldCursorID = cursorID;
}
Expand Down Expand Up @@ -366,10 +360,5 @@ private static bool IsCursorResourceAvailable()

return isCursorAvailable;
}

private static async void RunInUIThread(DispatcherQueue dispatcherQueue, Action action)
{
await dispatcherQueue.EnqueueAsync(action, DispatcherQueuePriority.Normal);
}
}
}
28 changes: 20 additions & 8 deletions Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Windows.System;
Expand Down Expand Up @@ -67,7 +68,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Action function, Dispatc
}
}))
{
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
}

return taskCompletionSource.Task;
Expand Down Expand Up @@ -116,7 +117,7 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<T> function, Dis
}
}))
{
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
}

return taskCompletionSource.Task;
Expand Down Expand Up @@ -149,7 +150,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func<Task> func
return awaitableResult;
}

return Task.FromException(new InvalidOperationException("The Task returned by function cannot be null."));
return Task.FromException(GetEnqueueException("The Task returned by function cannot be null."));
}
catch (Exception e)
{
Expand All @@ -173,7 +174,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
}
else
{
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
taskCompletionSource.SetException(GetEnqueueException("The Task returned by function cannot be null."));
}
}
catch (Exception e)
Expand All @@ -182,7 +183,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
}
}))
{
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
}

return taskCompletionSource.Task;
Expand Down Expand Up @@ -212,7 +213,7 @@ public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<Task
return awaitableResult;
}

return Task.FromException<T>(new InvalidOperationException("The Task returned by function cannot be null."));
return Task.FromException<T>(GetEnqueueException("The Task returned by function cannot be null."));
}
catch (Exception e)
{
Expand All @@ -236,7 +237,7 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
}
else
{
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
taskCompletionSource.SetException(GetEnqueueException("The Task returned by function cannot be null."));
}
}
catch (Exception e)
Expand All @@ -245,13 +246,24 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
}
}))
{
taskCompletionSource.SetException(new InvalidOperationException("Failed to enqueue the operation"));
taskCompletionSource.SetException(GetEnqueueException("Failed to enqueue the operation"));
}

return taskCompletionSource.Task;
}

return TryEnqueueAsync(dispatcher, function, priority);
}

/// <summary>
/// Creates an <see cref="InvalidOperationException"/> to return when an enqueue operation fails.
/// </summary>
/// <param name="message">The message of the exception.</param>
/// <returns>An <see cref="InvalidOperationException"/> with a specified message.</returns>
[MethodImpl(MethodImplOptions.NoInlining)]
private static InvalidOperationException GetEnqueueException(string message)
{
return new InvalidOperationException(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Linq;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.Input.GazeInteraction;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -21,7 +22,7 @@ public partial class XamlIslandsTest_Gaze
[TestInitialize]
public async Task Init()
{
await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
await App.Dispatcher.EnqueueAsync(() =>
{
var xamlItemsPanelTemplate = @"<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
Expand Down Expand Up @@ -65,7 +66,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
[TestCleanup]
public async Task Cleanup()
{
await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
await App.Dispatcher.EnqueueAsync(() =>
{
GazeInput.SetInteraction(_grid, Interaction.Disabled);
});
Expand All @@ -76,7 +77,7 @@ await App.Dispatcher.ExecuteOnUIThreadAsync(() =>
[Ignore]
public async Task Gaze_DoesNotCrashOnIslands()
{
await App.Dispatcher.ExecuteOnUIThreadAsync(async () =>
await App.Dispatcher.EnqueueAsync(async () =>
{
await Task.Delay(10000);

Expand Down