Niki
Niki

Reputation: 15867

How do you get an event for every unhandled exception in a mixed WinForms/WPF application

I want to get an event for every unhandled excpetion in my application, so I can write it to a log file. The application started as a Windows Forms application, where this can easily be done by subscribing the static System.Windows.Forms.Application.ThreadException event and calling System.Windows.Forms.Application.SetUnhandledExceptionMode.

In a pure WPF application, it's easy too, I just have to subscribe the System.Windows.Application instance's DispatcherUnhandledException event.

But how can I do this for a mixed application (i.e. a Windows Forms application with some WPF dialog windows and controls), where I don't have a global System.Windows.Application instance? System.Windows.Application.Current is null at startup, when I try to subscribe the event. I've tried creating my own instance of System.Windows.Application, but apparently it gets destroyed when the first WPF window is closed. If I try to open another WPF window after that, I get an EngineExecutionException.

Upvotes: 2

Views: 719

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

Have you tried this -

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

Upvotes: 1

Related Questions