dapplo/Dapplo.Microsoft.Extensions.Hosting

Winforms Application UnhandledExceptionEventHandler SetUnhandledExceptionMode

Opened this issue · 0 comments

Firstly thanks for this great project. It's exactly what I have searched and needed.
My issue is more a question.
I'm starting to use Dapplo with Winforms for DI, to have the possibility to use same code for a web api.

Until now, in my Winforms (GUI) programs I used - additional to Exception handling - following code in program.cs:

 System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException,false);
 public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            log.Error(e);
            MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
            // here you can handle the exception ...
        }
        public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            log.Error(e);
            MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
            // here you can handle the exception ...
        }

I have read that those events only work if Application.Run was started. Therefore I have the suspicion that those won't work with using: .UseWinFormsLifetime()
Is this correct? Are there any possibilities to include such central exception handling without losing user data in the form?
Thanks a lot for your great work!