1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-06-01 20:34:06 +02:00

Implement FlowExceptionsToTaskScheduler option

This commit is contained in:
Sergio Pedri 2022-06-03 16:57:15 +02:00
parent 79204ab489
commit dba9c4163e
2 changed files with 24 additions and 3 deletions

View File

@ -279,7 +279,14 @@ public bool CanExecute(object? parameter)
/// <inheritdoc/>
public void Execute(object? parameter)
{
_ = ExecuteAsync(parameter);
Task executionTask = ExecuteAsync(parameter);
// If exceptions shouldn't flow to the task scheduler, await the resulting task. This is
// delegated to a separate method to keep this one more compact in case the option is set.
if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
{
AwaitAndThrowIfFailed(executionTask);
}
}
/// <inheritdoc/>
@ -323,4 +330,13 @@ public void Cancel()
PropertyChanged?.Invoke(this, IsCancellationRequestedChangedEventArgs);
}
}
/// <summary>
/// Awaits an input <see cref="Task"/> and throws an exception on the calling context, if the task fails.
/// </summary>
/// <param name="executionTask">The input <see cref="Task"/> instance to await.</param>
internal static async void AwaitAndThrowIfFailed(Task executionTask)
{
await executionTask;
}
}

View File

@ -275,13 +275,18 @@ public bool CanExecute(object? parameter)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Execute(T? parameter)
{
_ = ExecuteAsync(parameter);
Task executionTask = ExecuteAsync(parameter);
if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
{
AsyncRelayCommand.AwaitAndThrowIfFailed(executionTask);
}
}
/// <inheritdoc/>
public void Execute(object? parameter)
{
_ = ExecuteAsync((T?)parameter);
Execute((T?)parameter);
}
/// <inheritdoc/>