0

I have general exception handling in my Winforms app like this:

static void Main()
{
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new CustomApplicationContext());

    Environment.Exit(-1);
}

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    ShowException(e.Exception);
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    ShowException(e.ExceptionObject as Exception);
    Thread.CurrentThread.Suspend();
}

What is the alternative to the Suspend method and what is the correct to use it in the above scenario?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • What problem are you going to solve with the call of Suspend? – Sir Rufo Sep 16 '18 at 06:14
  • @SirRufo It suspends the thread to stop the exception from throwing. – Ivan-Mark Debono Sep 16 '18 at 06:15
  • 2
    [You _really_ shouldn't want to do that anyway](https://stackoverflow.com/a/7821957/4934172). And here's [a more elaborate answer](https://stackoverflow.com/a/186879/4934172). If you insist, that second questions have other answers which suggest workarounds, but as said before, this is a very bad idea! – 41686d6564 stands w. Palestine Sep 16 '18 at 07:22
  • It is not obvious why you'd want to use it at all, forcing your user to terminate your crashed app with Task Manager is not a good practice. Something is wrong with ShowException(), perhaps, it should use a dialog to display the mishap. Like MessageBox. After which you pull the plug with Environment.Exit(). – Hans Passant Sep 16 '18 at 12:07

0 Answers0