Skip to content

Commit 612055b

Browse files
committed
Add async void notes in AsyncRelayCommand
1 parent fdda4c6 commit 612055b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

CommunityToolkit.Mvvm/Input/AsyncRelayCommand.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ public void Cancel()
337337
/// <param name="executionTask">The input <see cref="Task"/> instance to await.</param>
338338
internal static async void AwaitAndThrowIfFailed(Task executionTask)
339339
{
340+
// Note: this method is purposefully an async void method awaiting the input task. This is done so that
341+
// if an async relay command is invoked synchronously (ie. when Execute is called, eg. from a binding),
342+
// exceptions in the wrapped delegate will not be ignored or just become visible through the ExecutionTask
343+
// property, but will be rethrown in the original synchronization context by default. This makes the behavior
344+
// more consistent with how normal commands work (where exceptions are also just normally propagated to the
345+
// caller context), and avoids getting an app into an inconsistent state in case a method faults without
346+
// other components being notified. It is also possible to not await this task and to instead ignore exceptions
347+
// and then inspect them manually from the ExecutionTask property, by constructing an async command instance
348+
// using the AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler option. That will cause this call to
349+
// be skipped, and exceptions will just either normally be available through that property, or will otherwise
350+
// flow to the static TaskScheduler.UnobservedTaskException event if otherwise unobserved (eg. for logging).
340351
await executionTask;
341352
}
342353
}

0 commit comments

Comments
 (0)