diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs index 04dca4a5706..cd5a3c4910a 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs @@ -15,59 +15,47 @@ namespace Microsoft.Toolkit.Uwp.Helpers public static class DispatcherHelper { /// - /// Executes the given function asynchronously on UI thread of the main view. + /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. /// - /// Returned data type of the function /// Asynchronous function to be executed asynchronously on UI thread /// Dispatcher execution priority, default is normal - /// Awaitable Task with type - public static Task ExecuteOnUIThreadAsync(Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + /// Awaitable Task/> + public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { - return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); + return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); } /// /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. /// /// Returned data type of the function - /// View for the to be executed on - /// Asynchronous function to be executed asynchronously on UI thread + /// Synchronous function to be executed on UI thread /// Dispatcher execution priority, default is normal - /// Awaitable Task with type - public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + /// Awaitable Task + public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { - if (viewToExecuteOn == null) - { - throw new ArgumentNullException(nameof(viewToExecuteOn)); - } - - return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); + return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); } /// - /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. + /// Executes the given function asynchronously on UI thread of the main view. /// - /// View for the to be executed on /// Asynchronous function to be executed asynchronously on UI thread /// Dispatcher execution priority, default is normal /// Awaitable Task - public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { - if (viewToExecuteOn == null) - { - throw new ArgumentNullException(nameof(viewToExecuteOn)); - } - - return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); + return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); } /// /// Executes the given function asynchronously on UI thread of the main view. /// + /// Returned data type of the function /// Asynchronous function to be executed asynchronously on UI thread /// Dispatcher execution priority, default is normal - /// Awaitable Task - public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + /// Awaitable Task with type + public static Task ExecuteOnUIThreadAsync(Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); } @@ -89,17 +77,6 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecute return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); } - /// - /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. - /// - /// Asynchronous function to be executed asynchronously on UI thread - /// Dispatcher execution priority, default is normal - /// Awaitable Task/> - public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) - { - return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); - } - /// /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. /// @@ -115,96 +92,73 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToE throw new ArgumentNullException(nameof(viewToExecuteOn)); } - return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); + return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); } /// /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. /// - /// Returned data type of the function - /// Synchronous function to be executed on UI thread + /// View for the to be executed on + /// Asynchronous function to be executed asynchronously on UI thread /// Dispatcher execution priority, default is normal - /// Awaitable Task - public static Task ExecuteOnUIThreadAsync(Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + /// Awaitable Task + public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { - return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority); + if (viewToExecuteOn == null) + { + throw new ArgumentNullException(nameof(viewToExecuteOn)); + } + + return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); } /// - /// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher + /// Executes the given function asynchronously on given view's UI thread. Default view is the main view. /// /// Returned data type of the function - /// Dispatcher of a thread to run - /// Asynchrounous function to be executed asynchrounously on the given dispatcher + /// View for the to be executed on + /// Asynchronous function to be executed asynchronously on UI thread /// Dispatcher execution priority, default is normal /// Awaitable Task with type - public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { - if (function == null) + if (viewToExecuteOn == null) { - throw new ArgumentNullException(nameof(function)); + throw new ArgumentNullException(nameof(viewToExecuteOn)); } - var taskCompletionSource = new TaskCompletionSource(); - - _ = dispatcher.RunAsync(priority, async () => - { - try - { - var awaitableResult = function(); - - if (awaitableResult != null) - { - var result = await awaitableResult.ConfigureAwait(false); - - taskCompletionSource.SetResult(result); - } - else - { - taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); - } - } - catch (Exception e) - { - taskCompletionSource.SetException(e); - } - }); - - return taskCompletionSource.Task; + return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority); } /// /// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher /// /// Dispatcher of a thread to run - /// Asynchrounous function to be executed asynchrounously on the given dispatcher + /// Function to be executed asynchrounously on the given dispatcher /// Dispatcher execution priority, default is normal /// Awaitable Task - public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (function == null) { throw new ArgumentNullException(nameof(function)); } + if (dispatcher.HasThreadAccess) + { + function(); + + return Task.CompletedTask; + } + var taskCompletionSource = new TaskCompletionSource(); - _ = dispatcher.RunAsync(priority, async () => + _ = dispatcher.RunAsync(priority, () => { try { - var awaitableResult = function(); - - if (awaitableResult != null) - { - await awaitableResult.ConfigureAwait(false); - - taskCompletionSource.SetResult(null); - } - else - { - taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); - } + function(); + taskCompletionSource.SetResult(null); } catch (Exception e) { @@ -258,31 +212,77 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func< /// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher /// /// Dispatcher of a thread to run - /// Function to be executed asynchrounously on the given dispatcher + /// Asynchrounous function to be executed asynchrounously on the given dispatcher /// Dispatcher execution priority, default is normal /// Awaitable Task - public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { if (function == null) { throw new ArgumentNullException(nameof(function)); } - if (dispatcher.HasThreadAccess) + var taskCompletionSource = new TaskCompletionSource(); + + _ = dispatcher.RunAsync(priority, async () => { - function(); + try + { + var awaitableResult = function(); - return Task.CompletedTask; + if (awaitableResult != null) + { + await awaitableResult.ConfigureAwait(false); + + taskCompletionSource.SetResult(null); + } + else + { + taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + } + } + catch (Exception e) + { + taskCompletionSource.SetException(e); + } + }); + + return taskCompletionSource.Task; + } + + /// + /// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher + /// + /// Returned data type of the function + /// Dispatcher of a thread to run + /// Asynchrounous function to be executed asynchrounously on the given dispatcher + /// Dispatcher execution priority, default is normal + /// Awaitable Task with type + public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) + { + if (function == null) + { + throw new ArgumentNullException(nameof(function)); } - var taskCompletionSource = new TaskCompletionSource(); + var taskCompletionSource = new TaskCompletionSource(); - _ = dispatcher.RunAsync(priority, () => + _ = dispatcher.RunAsync(priority, async () => { try { - function(); - taskCompletionSource.SetResult(null); + var awaitableResult = function(); + + if (awaitableResult != null) + { + var result = await awaitableResult.ConfigureAwait(false); + + taskCompletionSource.SetResult(result); + } + else + { + taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null.")); + } } catch (Exception e) {