I'm doing some connecting-to-a-service-magic during the initialization of a ViewModel.
This logic is placed and awaited inside a Task.Run()
because I want the View to show prior to the completion of the returned task, thus I'm not awaiting it.
public override async Task Initialize()
{
await base.Initialize();
Task.Run(async () =>
{
await ConnectToServiceAsync();
// Do other stuff that requires the service connection
});
}
Now if anything goes wrong during that process and exceptions are thrown around, they will not be unwrapped (because the task is not awaited) and I can't catch them anywhere on that level (or at least I don't know how).
How can I handle exceptions that are thrown inside that Task.Run()
?
...I have a feeling that the solution it's related to .ConfigureAwait(false)
.
Unfortunately, I couldn't quite wrap my head around it so far.