File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -279,7 +279,14 @@ public bool CanExecute(object? parameter)
279
279
/// <inheritdoc/>
280
280
public void Execute ( object ? parameter )
281
281
{
282
- _ = ExecuteAsync ( parameter ) ;
282
+ Task executionTask = ExecuteAsync ( parameter ) ;
283
+
284
+ // If exceptions shouldn't flow to the task scheduler, await the resulting task. This is
285
+ // delegated to a separate method to keep this one more compact in case the option is set.
286
+ if ( ( this . options & AsyncRelayCommandOptions . FlowExceptionsToTaskScheduler ) == 0 )
287
+ {
288
+ AwaitAndThrowIfFailed ( executionTask ) ;
289
+ }
283
290
}
284
291
285
292
/// <inheritdoc/>
@@ -323,4 +330,13 @@ public void Cancel()
323
330
PropertyChanged ? . Invoke ( this , IsCancellationRequestedChangedEventArgs ) ;
324
331
}
325
332
}
333
+
334
+ /// <summary>
335
+ /// Awaits an input <see cref="Task"/> and throws an exception on the calling context, if the task fails.
336
+ /// </summary>
337
+ /// <param name="executionTask">The input <see cref="Task"/> instance to await.</param>
338
+ internal static async void AwaitAndThrowIfFailed ( Task executionTask )
339
+ {
340
+ await executionTask ;
341
+ }
326
342
}
Original file line number Diff line number Diff line change @@ -275,13 +275,18 @@ public bool CanExecute(object? parameter)
275
275
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
276
276
public void Execute ( T ? parameter )
277
277
{
278
- _ = ExecuteAsync ( parameter ) ;
278
+ Task executionTask = ExecuteAsync ( parameter ) ;
279
+
280
+ if ( ( this . options & AsyncRelayCommandOptions . FlowExceptionsToTaskScheduler ) == 0 )
281
+ {
282
+ AsyncRelayCommand . AwaitAndThrowIfFailed ( executionTask ) ;
283
+ }
279
284
}
280
285
281
286
/// <inheritdoc/>
282
287
public void Execute ( object ? parameter )
283
288
{
284
- _ = ExecuteAsync ( ( T ? ) parameter ) ;
289
+ Execute ( ( T ? ) parameter ) ;
285
290
}
286
291
287
292
/// <inheritdoc/>
You can’t perform that action at this time.
0 commit comments