Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Mar 24, 2020
1 parent ae4e43d commit 84b2b37
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action func

if (dispatcher.HasThreadAccess)
{
/* There is no need to manually handle the exceptions in this case.
* Since we're not scheduling a callback, exceptions thrown by the
* function in this path will correctly end up in the stack trace
* when the returned task is awaited anyway.
* This also saves a heap allocation, as the returned task is reused. */
function();

return Task.CompletedTask;
Expand All @@ -158,6 +163,7 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action func
try
{
function();

taskCompletionSource.SetResult(null);
}
catch (Exception e)
Expand Down Expand Up @@ -186,6 +192,10 @@ public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<

if (dispatcher.HasThreadAccess)
{
/* Just like with a simple action, we don't need to manually
* handle failures here. As we're not in a dispatched callback,
* exceptions thrown at this point will just show up correctly
* in the stack trace when the returned task is awaited. */
var result = function();

return Task.FromResult(result);
Expand Down

0 comments on commit 84b2b37

Please sign in to comment.