0

I have read these two pages from msdn: 1, 2

So I constructed this simple Web Forms app:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        new Thread(new ThreadStart(throwException)).Start();           
    }

    public static void throwException()
    {
        throw new Exception();
    }

    public static void UnhandledExHandler(object sender, UnhandledExceptionEventArgs t) 
    {
        MessageBox.Show("This is exception is unhandled.");            
    }
}

static class Program
{        
    static void Main()
    {            
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Form1.UnhandledExHandler);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

The problem with this is that after executing UnhandledExHandler, Forms1() constructor does not return but keeps throwing Exception, I mean it keeps calling throwException() on a new thread. The end user experience popping of MessageBox continuously. I know I must call Application.Exit() inside UnhandledExHandler to exit the app. But shouldnt this stop calling UnhandledExHandler after called once?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

2 Answers2

1

Assuming that all of your projects are running in the same appdomain, this will work correctly. We have this exact code encapsulated in a common DLL that is shared among numerous applications.

An additional suggestion: if this is used in Windows Forms applications, you probably also want to add a handler for System.Windows.Forms.Application.ThreadException.

This serves as a backstop when, for example, someone forgets to add exception handling to a control event.

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • My question if u think above should show `MessageBox` once, why it keeps showing again and again? Build simple Win Forms app and check the behavior – Mahesha999 Dec 26 '13 at 10:05
1

If you run your app without debugging you will get a message box and your app will be closed. You see many msg boxes because of the debugger work.

Your UnhandledExceptionEventArgs.IsTerminating is true, I guess you will not able to do anything with it in CurrentDomain.UnhandledException handler.

But you can handle it in ThreadException event handler.

Tony
  • 7,345
  • 3
  • 26
  • 34
  • Yes I checked the behavior out of VS, I run it by double clicking exe in `Debug` folder, so it showed a `MessageBox` then it displayed normal Program Stopped Working box and when I clicked Ok/Cancel the app was closed - is this desired or should it only return from `UnhandledExHandler` – Mahesha999 Dec 26 '13 at 10:15
  • In most cases your app will be unloaded (http://msdn.microsoft.com/ru-ru/library/system.unhandledexceptioneventargs.isterminating(v=vs.110).aspx). You can try solution from this post: http://stackoverflow.com/questions/186854/how-to-prevent-an-exception-in-a-background-thread-from-terminating-an-applicati – Tony Dec 26 '13 at 10:25
  • I tested behavior after adding `Application.Exit()` and it still does not close silently after dismissal of `MessageBox` instead it shows Windows error message box saying `This app stopped working...` – Mahesha999 Dec 26 '13 at 10:34
  • This is working for me: `Application.Exit(new CancelEventArgs(false));` – Tony Dec 26 '13 at 10:36
  • I do also checked by adding `new CancelEventArgs(false)` to `Application.Exit()` after `MessageBox` it shows `App Stopped Working...Check online...Close app` Windows message. – Mahesha999 Dec 26 '13 at 10:42