469,926 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

C# WinApp: AppDomain Unhandled Exception, handler does not force to close app

Shashi Sadasivan
1,435 Expert 1GB
Hi all,
I have got my windows app to handled any UI or unhandled exceptions.

following is the code

Expand|Select|Wrap|Line Numbers
  1. static class Program
  2.     {
  3.         /// <summary>
  4.         /// The main entry point for the application.
  5.         /// </summary>
  6.         [STAThread]
  7.         static void Main()
  8.         {
  9.             Application.EnableVisualStyles();
  10.             Application.SetCompatibleTextRenderingDefault(false);
  11.             Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  12.  
  13.             Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  14.             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  15.  
  16.             //try
  17.             //{
  18.                 Application.Run(new frmMainRibbon());
  19.  
  20.                 //Application.Run(new Login.frmMain());
  21.             //}
  22.             //catch (Exception ex)
  23.             //{
  24.             //    MessageBox.Show("Error: " + ex.Message, "Error");
  25.             //}
  26.         }
  27.  
  28.         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  29.         {
  30.             DialogResult dr = MessageBox.Show("Exception Occoured - unhandled : \n" + ((Exception)e.ExceptionObject).Message + "\nDo You want to report this problem?", "Application Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
  31.  
  32.             if (dr == DialogResult.Yes)
  33.             {
  34.  
  35.             }
  36.  
  37.         }
  38.         static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  39.         {
  40.             DialogResult dr = MessageBox.Show(e.Exception.Message + "\nDo You want to report this problem?", "Application Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
  41.  
  42.             if (dr == DialogResult.Yes)
  43.             {
  44.  
  45.             }
  46.         }
  47.     }
So what i did was since i did not handle an sql exception (when the database is not online) its supposed to be handled by CurrentDomain_UnhandledException

It works fine and the dialog is shown. However when the dialog is closed, the application is not forced to shut (even if i explicitly state Application.Close())
After the dialog box is closed by the user, windows shows the message that the application has closed unexpectedly and is looking for a resolution to this problem. Which only means that the application gave an error code to windows which is making windows look into its own solution site.

Any suggestions on how to avoid this?

thanks
Jan 18 '08 #1
1 5587
Shashi Sadasivan
1,435 Expert 1GB
Update:
In the CurrentDomain_UnhandledException i tried Application.Exit();
However that did not change anything and windows still wanted to check for a solution.

I then used Environment.Exit(-1); and that was a solution. (it worked)

However, I have no idea about the exit codes. All i know that 0 is supposed to be a failure and 1 as a success (from my C days)
but changing this exit code dose not affect anything (even with a -1, 0 or 1) windows dosent do anything. So whats a valid exit code?
Jan 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by terry | last post: by
2 posts views Thread by guy | last post: by
22 posts views Thread by JPSutor | last post: by
2 posts views Thread by Bob | last post: by
4 posts views Thread by illegal.prime | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.