diff --git a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs index cd5a3c4910a..83b59147b4f 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs @@ -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; @@ -158,6 +163,7 @@ public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action func try { function(); + taskCompletionSource.SetResult(null); } catch (Exception e) @@ -186,6 +192,10 @@ public static Task AwaitableRunAsync(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);