Skip to content

Commit

Permalink
feat: suppress TaskCanceledException if token was canceled.
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Oct 29, 2023
1 parent 1a19608 commit 1d33294
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Prism.Core/Commands/AsyncDelegateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,23 @@ public bool CanExecute()
/// <param name="parameter">Command Parameter</param>
protected override async void Execute(object? parameter)
{
await Execute(_getCancellationToken());
var cancellationToken = _getCancellationToken();
try
{
await Execute(cancellationToken)
.ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Do nothing... the Task was cancelled
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;

ExceptionHandler.Handle(ex, parameter);
}
}

/// <summary>
Expand Down
8 changes: 7 additions & 1 deletion src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,15 @@ public bool CanExecute(T parameter)
/// <param name="parameter">Command Parameter</param>
protected override async void Execute(object? parameter)
{
var cancellationToken = _getCancellationToken();
try
{
await Execute((T)parameter!, _getCancellationToken());
await Execute((T)parameter!, cancellationToken)
.ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Do nothing... the Task was cancelled
}
catch (Exception ex)
{
Expand Down

0 comments on commit 1d33294

Please sign in to comment.