tags:

views:

57

answers:

2

so i came across this interesting article on WPF exception handling: http://srtsolutions.com/public/item/251263

it works by declaring DispatcherUnhandledException handler in xaml <application> node.

but what if a WPF window is launched from win forms application?

where can i declare a general exception handler?

The problem is that when WPF crashes, it brings down the whole WinForms app with it.

*Edit what if instead of launching the WPF window directly, i launched an "Application" which than defined a start window?? is that possible/advisable?

A: 

I believe the traditional "global" exception handlers for Windows Forms would apply here:

Hook the thread exception as the first line in Main:

Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException); 
David
thanks, that is actually already implemented. but any WPF exception still nukes the container winforms app.
Sonic Soul
+2  A: 

Attach your handler to the <obj>.Dispatcher.UnhandledException event, where <obj> is any WPF control, or another DispatcherObject that is created on the same thread.

Fyodor Soikin
any way you can elaborate on how to actually assign the handler to the dispatcher? UnhandledException is not really a property of it..
Sonic Soul
It's not a property, but an event. Use the usual `+=` syntax. Check out documentation: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.unhandledexception.aspx
Fyodor Soikin
poor choice of words, i meant that, that event is not available on my windows dispatcher object.
Sonic Soul
How is that? I mean, how do you know that it's not available?
Fyodor Soikin
actually, it is there, my mistake.
Sonic Soul