Skip to content

Commit 33c58e3

Browse files
committed
Remove try/catch when awaiting tasks, ignore exceptions
This allows exceptions to remain unobserved and propagate to TaskScheduler.UnobservedTaskException. It will also be more efficient, as there is no longer an exception being thrown and handled if there's one.
1 parent 98fc907 commit 33c58e3

File tree

5 files changed

+15
-36
lines changed

5 files changed

+15
-36
lines changed

CommunityToolkit.Mvvm.SourceGenerators/EmbeddedResources/INotifyPropertyChanged.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,7 @@ private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNo
389389

390390
async void MonitorTask()
391391
{
392-
try
393-
{
394-
await newValue!;
395-
}
396-
catch
397-
{
398-
}
392+
await global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__TaskExtensions.GetAwaitableWithoutEndValidation(newValue!);
399393

400394
if (ReferenceEquals(taskNotifier.Task, newValue))
401395
{

CommunityToolkit.Mvvm.SourceGenerators/EmbeddedResources/ObservableObject.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,7 @@ private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNo
437437

438438
async void MonitorTask()
439439
{
440-
try
441-
{
442-
await newValue!;
443-
}
444-
catch
445-
{
446-
}
440+
await global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__TaskExtensions.GetAwaitableWithoutEndValidation(newValue!);
447441

448442
if (ReferenceEquals(taskNotifier.Task, newValue))
449443
{

CommunityToolkit.Mvvm/ComponentModel/ObservableObject.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
using System.Diagnostics.CodeAnalysis;
2020
using System.Runtime.CompilerServices;
2121
using System.Threading.Tasks;
22+
using CommunityToolkit.Mvvm.ComponentModel.__Internals;
23+
24+
#pragma warning disable CS0618
2225

2326
namespace CommunityToolkit.Mvvm.ComponentModel;
2427

@@ -525,14 +528,8 @@ private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNo
525528
// which would result in a confusing behavior for users.
526529
async void MonitorTask()
527530
{
528-
try
529-
{
530-
// Await the task and ignore any exceptions
531-
await newValue!;
532-
}
533-
catch
534-
{
535-
}
531+
// Await the task and ignore any exceptions
532+
await newValue!.GetAwaitableWithoutEndValidation();
536533

537534
// Only notify if the property hasn't changed
538535
if (ReferenceEquals(taskNotifier.Task, newValue))

CommunityToolkit.Mvvm/Input/AsyncRelayCommand.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using System.Runtime.CompilerServices;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using CommunityToolkit.Mvvm.ComponentModel.__Internals;
11+
12+
#pragma warning disable CS0618
1013

1114
namespace CommunityToolkit.Mvvm.Input;
1215

@@ -213,13 +216,7 @@ private set
213216

214217
static async void MonitorTask(AsyncRelayCommand @this, Task task)
215218
{
216-
try
217-
{
218-
await task;
219-
}
220-
catch
221-
{
222-
}
219+
await task.GetAwaitableWithoutEndValidation();
223220

224221
if (ReferenceEquals(@this.executionTask, task))
225222
{

CommunityToolkit.Mvvm/Input/AsyncRelayCommand{T}.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using System.Runtime.CompilerServices;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using CommunityToolkit.Mvvm.ComponentModel.__Internals;
11+
12+
#pragma warning disable CS0618
1013

1114
namespace CommunityToolkit.Mvvm.Input;
1215

@@ -197,13 +200,7 @@ private set
197200

198201
static async void MonitorTask(AsyncRelayCommand<T> @this, Task task)
199202
{
200-
try
201-
{
202-
await task;
203-
}
204-
catch
205-
{
206-
}
203+
await task.GetAwaitableWithoutEndValidation();
207204

208205
if (ReferenceEquals(@this.executionTask, task))
209206
{

0 commit comments

Comments
 (0)