Skip to content

Commit

Permalink
APIs reordered
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Mar 24, 2020
1 parent 9be83e2 commit ae4e43d
Showing 1 changed file with 99 additions and 99 deletions.
198 changes: 99 additions & 99 deletions Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,47 @@ namespace Microsoft.Toolkit.Uwp.Helpers
public static class DispatcherHelper
{
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">Returned data type of the function</typeparam>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
public static Task<T> ExecuteOnUIThreadAsync<T>(Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
/// <returns>Awaitable Task/></returns>
public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync<T>(CoreApplication.MainView, function, priority);
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}

/// <summary>
/// Executes the given function asynchronously on given view's UI thread. Default view is the main view.
/// </summary>
/// <typeparam name="T">Returned data type of the function</typeparam>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on </param>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="function">Synchronous function to be executed on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToExecuteOn, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
/// <returns>Awaitable Task </returns>
public static Task<T> ExecuteOnUIThreadAsync<T>(Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (viewToExecuteOn == null)
{
throw new ArgumentNullException(nameof(viewToExecuteOn));
}

return viewToExecuteOn.Dispatcher.AwaitableRunAsync<T>(function, priority);
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on </param>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task</returns>
public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
public static Task ExecuteOnUIThreadAsync(Func<Task> 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);
}

/// <summary>
/// Executes the given function asynchronously on UI thread of the main view.
/// </summary>
/// <typeparam name="T">Returned data type of the function</typeparam>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task</returns>
public static Task ExecuteOnUIThreadAsync(Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
public static Task<T> ExecuteOnUIThreadAsync<T>(Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}
Expand All @@ -89,17 +77,6 @@ public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecute
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
}

/// <summary>
/// Executes the given function asynchronously on given view's UI thread. Default view is the main view.
/// </summary>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task/></returns>
public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}

/// <summary>
/// Executes the given function asynchronously on given view's UI thread. Default view is the main view.
/// </summary>
Expand All @@ -115,96 +92,73 @@ public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToE
throw new ArgumentNullException(nameof(viewToExecuteOn));
}

return viewToExecuteOn.Dispatcher.AwaitableRunAsync<T>(function, priority);
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
}

/// <summary>
/// Executes the given function asynchronously on given view's UI thread. Default view is the main view.
/// </summary>
/// <typeparam name="T">Returned data type of the function</typeparam>
/// <param name="function">Synchronous function to be executed on UI thread</param>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on </param>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task </returns>
public static Task<T> ExecuteOnUIThreadAsync<T>(Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
/// <returns>Awaitable Task</returns>
public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func<Task> 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);
}

/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">Returned data type of the function</typeparam>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/></param>
/// <param name="function">Asynchrounous function to be executed asynchrounously on the given dispatcher</param>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on </param>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToExecuteOn, Func<Task<T>> 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<T>();

_ = 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);
}

/// <summary>
/// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher
/// </summary>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/></param>
/// <param name="function">Asynchrounous function to be executed asynchrounously on the given dispatcher</param>
/// <param name="function"> Function to be executed asynchrounously on the given dispatcher</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task</returns>
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func<Task> 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<object>();

_ = 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)
{
Expand Down Expand Up @@ -258,31 +212,77 @@ public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<
/// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher
/// </summary>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/></param>
/// <param name="function"> Function to be executed asynchrounously on the given dispatcher</param>
/// <param name="function">Asynchrounous function to be executed asynchrounously on the given dispatcher</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task</returns>
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (function == null)
{
throw new ArgumentNullException(nameof(function));
}

if (dispatcher.HasThreadAccess)
var taskCompletionSource = new TaskCompletionSource<object>();

_ = 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;
}

/// <summary>
/// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher
/// </summary>
/// <typeparam name="T">Returned data type of the function</typeparam>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/></param>
/// <param name="function">Asynchrounous function to be executed asynchrounously on the given dispatcher</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (function == null)
{
throw new ArgumentNullException(nameof(function));
}

var taskCompletionSource = new TaskCompletionSource<object>();
var taskCompletionSource = new TaskCompletionSource<T>();

_ = 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)
{
Expand Down

0 comments on commit ae4e43d

Please sign in to comment.