none
Application.ThreadException not fire when raise exception in ContextMenuStrip.Closed event. RRS feed

  • Question

  • program.cs

        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.ThreadException += Application_ThreadException;
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
    
            static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                MessageBox.Show(e.Exception.ToString());
            }
        }
    

    Form1.cs

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                this.ContextMenuStrip = contextMenuStrip1;
            }
    
            private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
            {   
                throw new Exception("Closed Exception");
            }
        }

    1. Build and Run (Ctrl + F5).

    2. Mouse right button click on Form.

    3. Show ContextMenu and Form Click (not ContextMenu).

    4. Raise unhandled event and application kill.

    Why not fire Application.ThreadException event?

    But.

    3. Show ContextMenu and ContextMenuItem Click

    4. Fire Application.ThreadException event and keep appliation.


    • Edited by Jongho, Kim Wednesday, May 28, 2014 5:20 AM
    Tuesday, May 27, 2014 12:23 AM

Answers

  • "Yes, I can catch the exception using your code. But application is terminated."

    And that's the normal thing to do when an unhandled exception occurs. Catching exceptions via ThreadException and leaving the application running is bad practice.

    "I wonder why not fire Application.ThreadException event with my code. I think that is bug."

    Looks like. What happens is that the ContextMenuStrip installs a message filter to detect when you click outside the context menu window. When that happens the message filter closes the context menu and in turn that causes the Closed event to be raised. But the Application level exception handling isn't done when message filters are run and the exception escapes.

    Tuesday, June 3, 2014 5:21 AM
    Moderator

All replies

  • Hi Nuwanamj,

    I tested your code and it worked on my side. Did you try your code on another computer to see this problem persists? If it doesn’t work, can you please post the project here to help reproduce. Thank you for understanding.

    By the way, you can try the example on the following link. http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception(v=vs.110).aspx.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 28, 2014 2:22 AM
    Moderator
  • Thank you.

    Perhaps the code that you have tested will not work in the following steps.

    If you close the ContextMenu by clicking on the Form without clicking an area of ​​the ContextMenu,  Application.ThreadException does not occur.

    Wednesday, May 28, 2014 5:24 AM
  • Hi Nuwanamj,

    I modify the code and test it. I can catch the exception as you wish. Please see the following code and tell me if it meets your requirement.

    [STAThread]
    
            static void Main()
    
            {
    
                //Application.ThreadException += Application_ThreadException;
    
                //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    
                AppDomain.CurrentDomain.UnhandledException +=
    
                    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
    
                Application.EnableVisualStyles();
    
                Application.SetCompatibleTextRenderingDefault(false);
    
                Application.Run(new Form1());
    
            }
    
      private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    
            {
    
                MessageBox.Show(e.ExceptionObject.ToString());
    
            }

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, May 30, 2014 6:38 AM
    Moderator
  • Hi Werro wong.

    Yes, I can catch the exception using your code. But application is terminated.

    I don't want AppDomain.CurrentDomain.UnhandledException event.

    I wonder why not fire Application.ThreadException event with my code.

    I think that is bug.

    Thank you.

    Tuesday, June 3, 2014 12:03 AM
  • "Yes, I can catch the exception using your code. But application is terminated."

    And that's the normal thing to do when an unhandled exception occurs. Catching exceptions via ThreadException and leaving the application running is bad practice.

    "I wonder why not fire Application.ThreadException event with my code. I think that is bug."

    Looks like. What happens is that the ContextMenuStrip installs a message filter to detect when you click outside the context menu window. When that happens the message filter closes the context menu and in turn that causes the Closed event to be raised. But the Application level exception handling isn't done when message filters are run and the exception escapes.

    Tuesday, June 3, 2014 5:21 AM
    Moderator