I am having a case where an exception thrown in UI thread doesn't get catched in the calling thread.
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows;
namespace SynchronisationContextAndExceptionWPF
{
public partial class MainWindow : Window
{
private readonly SynchronizationContext _synchronizationContext;
public MainWindow()
{
InitializeComponent();
_synchronizationContext = SynchronizationContext.Current;
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
try
{
_synchronizationContext.Send(
x =>
{
try
{
DoSomethingOnUiThreadThatThrowsException();
}
catch (Exception)
{
Debug.WriteLine("Catched Exception in thread that threw it.");
throw;
}
}, null);
}
catch (Exception)
{
Debug.WriteLine("Catched Exception in thread that calles Send-Method.");
throw;
}
}
private static void DoSomethingOnUiThreadThatThrowsException()
{
throw new Exception("Any Exception...");
}
}
}
First I thought that cannot possible (all documentation I found said that I can catch exceptions there).
After some reasearch I found the problem: My application uses an UnhandledExceptionHandler. That handles the DispatcherUnhandledException-Event. I am showing some information to the user and set e.Handled = true;:
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
namespace SynchronisationContextAndExceptionWPF
{
public partial class App : Application
{
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
}
private static void App_DispatcherUnhandledException(
object sender,
DispatcherUnhandledExceptionEventArgs e)
{
Debug.WriteLine("Catched Exception in UnhandledExceptionHandler.");
// This line makes the difference:
e.Handled = true;
}
}
}
So the question: why is the DispatcherUnhandledException-Event raised even if I handle it?
How would you solve this situation?
If you have a lot of controls, you can generate a new class which remenbers the special exception variable. So you only need to change the initialization of your _synchronizationContext (hopefully only once at your base class of your controls).
public partial class MainWindow : Window
{
private readonly MySynchronizationContext _synchronizationContext;
public MainWindow()
{
InitializeComponent();
_synchronizationContext = new MySynchronizationContext(SynchronizationContext.Current);
}
private void button_Click(object sender, RoutedEventArgs e)
{
try
{
_synchronizationContext.Send(
x =>
{
DoSomethingOnUiThreadThatThrowsException();
}, null);
}
catch (Exception)
{
Debug.WriteLine("Catched Exception in thread that calles Send-Method.");
throw;
}
}
private static void DoSomethingOnUiThreadThatThrowsException()
{
throw new Exception("Any Exception...");
}
}
class MySynchronizationContext
{
SynchronizationContext innerContext;
public MySynchronizationContext(SynchronizationContext ctx)
{
innerContext = ctx;
}
public virtual void Send(SendOrPostCallback d, object state)
{
Exception threadException = null;
try
{
innerContext.Send(_ =>
{
try
{
d.Invoke(state);
}
catch (Exception exception)
{
threadException = exception;
}
}, null);
}
catch (Exception ex)
{
}
if (threadException != null)
{
throw new Exception("Synchronization error", threadException);
}
}
}
Inside your lambda expression you can set an Exception variable and check this variable later at the calling thread. If it was set, then throw exception at calling thread.
private void button_Click(object sender, RoutedEventArgs e)
{
Exception threadException = null;
try
{
_synchronizationContext.Send(
x =>
{
try
{
DoSomethingOnUiThreadThatThrowsException();
}
catch (Exception ex)
{
Debug.WriteLine("Catched Exception in thread that threw it.");
threadException = ex;
//throw; --> don't throw exception here; otherwise you will get DispatcherUnhandledException twice.
}
}, null);
}
catch (Exception)
{
Debug.WriteLine("Catched Exception in thread that calles Send-Method.");
throw;
}
if(threadException != null)
{
Debug.WriteLine("Catched Exception in thread that calles Send-Method.");
throw threadException; //throw you previously catched exception here.
}
}
Kind Regards,
Daniel
Related
I'm following on with a tutorial detailing handling events thrown in multiple subscribers to an event. However when the code is run, the TargetInvocationException catch block isn't catching the exceptions thrown in the method body of the subscribers: throw new Exception("Hello") and throw new Exception("World") respectively.
Instead I get an unhandled exception error in the first listener, predictably at throw new Exception("Hello") in private static void AlarmListener.
What is it that I'm doing incorrectly when attempting to catch the invoked method's exceptions?
class AggregatingExceptions
{
public void Main()
{
//create the alarm
AlarmAndLocation alarm = new AlarmAndLocation();
//subscribe the listeners
alarm.OnAlarmRaised += AlarmListener;
alarm.OnAlarmRaised += AlarmListener2;
try
{
alarm.RaiseAlarm("Kitchen");
}
catch (AggregateException agg)
{
foreach (Exception ex in agg.InnerExceptions)
{
Console.WriteLine(ex.Message);
}
}
}
private static void AlarmListener(object sender, AlarmEventArgs e)
{
Console.WriteLine("Alarm listener 1 called.");
throw new Exception("Hello");
}
private static void AlarmListener2(object sender, AlarmEventArgs e)
{
Console.WriteLine("Alarm listener 2 called.");
throw new Exception("World");
}
}
public class AlarmAndLocation
{
public event EventHandler<AlarmEventArgs> OnAlarmRaised = delegate { };
public List<Exception> exceptionList = new List<Exception>();
public void RaiseAlarm(string location)
{
foreach (Delegate handler in OnAlarmRaised.GetInvocationList())
{
try
{
handler.DynamicInvoke(this, new AlarmEventArgs(location));
}
catch (TargetInvocationException ex)
{
exceptionList.Add(ex.InnerException);
}
}
if(exceptionList.Count > 0)
{
throw new AggregateException(exceptionList);
}
}
}
public class AlarmEventArgs : EventArgs
{
public string Location { get; set; }
public AlarmEventArgs(string location)
{
Location = location;
}
}
I have problems calling Form objects from another class.
In Form class I have this:
public void reloadMapOverlay(GMapOverlay overlay)
{
try
{
Invoke(new Action(() => this.map_Box.Overlays.Add(overlay)));
}
catch (Exception e)
{
Console.WriteLine("reloadMapOverlay: {0}", e);
this.setError("reloadMapOverlay: " + e);
}
}
And in another class (Map_custom.cs):
route.Points.Add(coords.get_position());
overlay.Routes.Add(route);
mainForm.reloadMapOverlay(overlay);
When mainForm.reloadMapOverlay executes, vb gives me and object in use an exception
System.InvalidOperationException
in overlay object.
What can I can do?
i try adding dispacher in mainForm, ->
public Dispatcher dispacher = Dispatcher.CurrentDispatcher;
public void reloadMapOverlay(GMapOverlay overlay)
{
try
{
dispacher.Invoke(new Action(() => this.map_Box.Overlays.Add(overlay)));
}
catch (Exception e)
{
Console.WriteLine("reloadMapOverlay: {0}", e);
this.setError("reloadMapOverlay: " + e);
}
}
and nothing.
i have
public void reloadMapOverlay(GMapOverlay overlay)
{
try
{
Invoke(new Action(() => this.map_Box.Overlays.Add(overlay)));
}
catch (Exception e)
{
Console.WriteLine("reloadMapOverlay: {0}", e);
this.setError("reloadMapOverlay: " + e);
}
} in mainForm,
then, in another class (Map_Custom.cs) i have a function
public void addRoute()
{
if (!coords.isWrongCoords())
{
Console.WriteLine("Route");
route.Points.Add(coords.get_position());
overlay.Routes.Add(route);
mainForm.reloadMapOverlay(overlay);
}
}
and in mainFrom i have a function (inside one thread) where i call
Thread addRoute = new Thread(new ThreadStart(map.addRoute));
addRoute.IsBackground = true;
addRoute.Start();
I have a function in my logical Layer that insert information by another thread and I want if unknown exception happen in that thread throw it but the problem is that in my application UnhandledExceptionEventHandler that get general exception it gives this error and i also don't want to shutdown the application:
The calling thread must be STA, because many UI components require this
///////////////////// this is my function in logical layer //////////
public string MahaleInsert(Mahales Mahale)
{
Thread t = new Thread(delegate()
{
try
{
Mahale.OutMessage = DA_Agency.MahaleInsertDB(Mahale);
Mahale.OutMessage = "SuccessInsert";
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("violation of unique key constraint"))
Mahale.OutMessage = "MahaleInsUniqueError";
else
throw;
}
});
t.Start();
t.Join();
return Mahale.OutMessage;
}
//////////////////////////// this in my aplication level //////////////////
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string messsage = GeneralMethod.CatchException(e.ExceptionObject as Exception);
MessageClass.MessageBox(messsage);
}
catch
{
}
}
Try To do the following :
public string MahaleInsert(Mahales Mahale)
{
Thread t = new Thread(new ThreadStart(ThreadBody));
t.Start();
t.Join();
return Mahale.OutMessage;
}
[STAThread]
void ThreadBody()
{
try
{
Mahale.OutMessage = DA_Agency.MahaleInsertDB(Mahale);
Mahale.OutMessage = "SuccessInsert";
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("violation of unique key constraint"))
Mahale.OutMessage = "MahaleInsUniqueError";
else
throw;
}
}
//////////////////////////// this in my aplication level //////////////////
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string messsage = GeneralMethod.CatchException(e.ExceptionObject as Exception);
MessageClass.MessageBox(messsage);
}
catch
{
}
}
I am using a singleton pattern for a global event manager class that is not handling exceptions in an acceptable manner.
If an exception is thrown in the code called by one of the events being executed, I always get a Exception has been thrown by the target of an invocation. error. This error contains no information related to the original exception, making it extremely difficult to debug any errors.
Is there some way to pass the original exception information back to the event manager?
public class ApplicationSettings
{
private static EventManager _manager = new EventManager();
public static EventManager EventManager
{
get { return _manager; }
}
}
The event manager class:
public class EventManager
{
public event EventHandler<ReportExecutionArgs> ExecuteReportCurrentPage;
public event EventHandler<ReportExecutionArgs> ExecuteReportNewPage;
public virtual void OnExecuteReportCurrentPage(object sender, ReportExecutionArgs e)
{
try
{
if (this.ExecuteReportCurrentPage != null)
this.ExecuteReportCurrentPage(sender, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public virtual void OnExecuteReportNewPage(object sender, ReportExecutionArgs e)
{
try
{
if (this.ExecuteReportNewPage != null)
this.ExecuteReportNewPage(sender, e);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
Some other class will handle these events
ApplicationSettings.EventManager.ExecuteReportNewPage += new EventHandler<ReportExecutionArgs>(reportOpenedNewPage);
private void reportOpenedNewPage(object sender, ReportExecutionArgs e)
{
//something in this code throws an error
LitePage page = new LitePage();
_tabs.AddPage(page);
Report report = setReport(page, e);
}
EDIT
Just to clarify, the try/catch blocks in the OnExecuteReport methods are not catching the exception.
A TargetInvocationException such as the one you describe will almost always have the originating exception in it's InnerException.
I'm trying to handle the Timer's exception. It would be nice if the class had something like HandlerExceptionEvent so that we could add some event to log something or stop the timer.
PS: I don't want to add a try/catch block inside ElapsedEventHandler().
class Program
{
static void Main(string[] args) {
System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Start();
System.Threading.Thread.Sleep(10000);
t.Stop();
Console.WriteLine("\nDone.");
Console.ReadLine();
}
static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
Console.WriteLine("Ping!");
throw new Exception("Error!");
}
}
PS: I don't want to add "try/catch Exception" inside ElapsedEventHandler()
Since the Timer class doesn't support such an event how would you otherwise catch an exception?
If you insist on using the Timer class then perhaps this is your only option:
var t = new System.Timers.Timer(1000);
t.Elapsed += (sender, e) => {
try
{
t_Elapsed(sender, e);
}
catch (Exception ex)
{
// Error handling here...
}
};
This way the actual handler t_Elapsed doesn't contain any error handling and you can create a wrapper class for the Timer class that hides this implementation detail and in turn provides an event for exception handling.
Here's one way to do that:
class ExceptionHandlingTimer
{
public event Action<Exception> Error;
System.Timers.Timer t;
public ExceptionHandlingTimer(double interval)
{
t = new System.Timers.Timer(interval);
}
public void Start()
{
t.Start();
}
public void AddElapsedEventHandler(ElapsedEventHandler handler)
{
t.Elapsed += (sender, e) =>
{
try
{
handler(sender, e);
}
catch (Exception ex)
{
if (Error != null)
{
Error(ex);
}
else
{
throw;
}
}
};
}
}