Reputation: 4313
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;
}
Upvotes: 0
Views: 844
Reputation: 456557
You should not use Task.Factory.StartNew
(use Task.Run
instead). Also, do not use ContinueWith
(use await
instead).
Applying both of these guidelines:
try
{
await Task.Run(() =>
{
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();
});
}
catch (Exception ex)
{
// You're back on the UI thread here
... // handle exception
}
Upvotes: 2
Reputation: 3979
You are using .Net version 4.5.2 so your languageversion should be c# 5. So you could be able todo the following:
try
{
Task t1 = await Task.Factory.StartNew(() => {
//Do you stuff which may cause exception
})
}
catch ()
{}
The await keyword causes that you must mark your method with async. But it won't block and is very intuitive. If this don't work use the idea from Dmitry Bychenko:
Task t1 = await Task.Factory.StartNew(() => {
//Do you stuff which may cause exception
}).ContinueWith(t=>ShowError(), TaskContinuationOptions.OnlyOnFaulted);
Upvotes: 0
Reputation: 156
You can subscribe to the TaskScheduler.UnobservedTaskException
event
Upvotes: 0