How can I easily handle all exceptions that happens inside the task that I am running without blocking the UI thread.
I found a lot of different solutions but they all involve the wait() function and this blocks the whole program.
The task is running async so it should just send a message to the UI thread saying that it has an exceptions so that the UI thread can handle it. (Maybe an event that I can hook on?)
This is the code I have now that blocks the UI Thread:
var task = Task.Factory.StartNew(() =>
{
if (_proxy != null)
{
_gpsdService.SetProxy(_proxy.Address, _proxy.Port);
if (_proxy.IsProxyAuthManual)
{
_gpsdService.SetProxyAuthentication(_proxy.Username,
StringEncryption.DecryptString(_proxy.EncryptedPassword, _encryptionKey).ToString());
}
}
_gpsdService.OnLocationChanged += GpsdServiceOnOnLocationChanged;
_gpsdService.StartService();
});
try
{
task.Wait();
}
catch (AggregateException ex)
{
if (ex.InnerException != null)
{
throw ex.InnerException;
}
throw;
}
task.ContinueWith(task => {... }, TaskContinuationOptions.OnlyOnFaulted);ViewModelshould provide a routine for continuation (e.g. if any exception thrown It's aModelwho should response for it):task.ContinueWith(task => SomeRoutine(task.Exception), TaskContinuationOptions.OnlyOnFaulted);. It doesn't look that easy as in sequential code. What if the model's run, say, 5Tasks and 3d with 4th failed. Shall we throw 3d task's exception, or 4th one, comibine them (in which order)?