locked
Application.Exit vs "Red X"

    Question

  • I have searched the archives & found related info, but nothing that specifically addresses the
    diffenece between using Application.Exit() vs the 'red x'.

    I have always used Application.Exit() activated by an Exit button or Exit menu item -- very successfully until now.

    Today I ran into a problem. Application.Exit() is not calling the form.Closing event but the 'red x' does.

    I tried Environement.Exit() knowing this is a more 'harsh' method -- again no form.Closing event.

    Next I tried this.Close() which does activate the form.Closing event and my app is happy, BUT:

    is this a known limitation of Application.Exit() & I am just now catching up? Of course there is always something new to learn. (FYI - I normally do not use the form.Closing event)

    OR

    is there something 'different' about my new app that needs further investigation?


    Anyone have comments/observations.  TIA, Joe
    Wednesday, December 09, 2009 5:36 PM

Answers

  • This is explicitly said in the documentation.

    Read the whole page. 

    Closing and Closed are obsolete, according to that same page.  What should you use?  It says in the documentation.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Proposed as answer by JohnGrove Thursday, December 10, 2009 11:22 PM
    • Marked as answer by Harry Zhu Wednesday, December 16, 2009 1:50 AM
    Wednesday, December 09, 2009 5:44 PM
  • Hi,

    FormClosing and Closing are different.

    Please try to run the code bellow , we can see when Application.Exit() is called , FormClosing and FormClosed will be sent. when we uncomment this.Close(), all these event will be sent.

    So please try to use FormClosing event handler instead of Closing event handler.

           private void Form1_Load(object sender, EventArgs e)
            {
                this.Closing += new CancelEventHandler(Form1_Closing);
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
                this.Closed += new EventHandler(Form1_Closed);
                this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);                    
            }
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                MessageBox.Show("formclosing was sent");           
            }
            void Form1_Closing(object sender, CancelEventArgs e)
            {
                MessageBox.Show("closing was sent");            
            }
            void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                MessageBox.Show("formclosed");
              
            }
            void Form1_Closed(object sender, EventArgs e)
            {
                MessageBox.Show("closed");           
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //this.Close();
                Application.Exit();
            }

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Wednesday, December 16, 2009 1:50 AM
    Friday, December 11, 2009 7:26 AM

All replies

  • This is explicitly said in the documentation.

    Read the whole page. 

    Closing and Closed are obsolete, according to that same page.  What should you use?  It says in the documentation.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Proposed as answer by JohnGrove Thursday, December 10, 2009 11:22 PM
    • Marked as answer by Harry Zhu Wednesday, December 16, 2009 1:50 AM
    Wednesday, December 09, 2009 5:44 PM
  • Thanks for the reply Dave.

    I read the documentation as provided by your link, but am slightly more confused than before.

    For example, the documentation says:

    Exit
    raises the following events and performs the associated conditional actions:

    • A FormClosing event is raised for every form .......................


      And then the Note below says:

      The Exit method does not raise the Closed and Closing events, which are obsolete as of .NET Framework 2.0


      Finally the example uses Application.Exit()


      So, it seems as though Exit did raise Closing in .Net 1.x but Closing/Closed were made obsolete in .Net 2.0 which means the statement "a formclosing even is raised for every form ..." only applies to .Net 1.x.

      Therefore, I conclude that Closing & Closed events should not be used in new applications and legacy code which did use these events will need some work. This is exactly the case I have -- legacy code being used
      as a class within a new app.










    Wednesday, December 09, 2009 10:49 PM
  • Hi,

    FormClosing and Closing are different.

    Please try to run the code bellow , we can see when Application.Exit() is called , FormClosing and FormClosed will be sent. when we uncomment this.Close(), all these event will be sent.

    So please try to use FormClosing event handler instead of Closing event handler.

           private void Form1_Load(object sender, EventArgs e)
            {
                this.Closing += new CancelEventHandler(Form1_Closing);
                this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
                this.Closed += new EventHandler(Form1_Closed);
                this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);                    
            }
            void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                MessageBox.Show("formclosing was sent");           
            }
            void Form1_Closing(object sender, CancelEventArgs e)
            {
                MessageBox.Show("closing was sent");            
            }
            void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                MessageBox.Show("formclosed");
              
            }
            void Form1_Closed(object sender, EventArgs e)
            {
                MessageBox.Show("closed");           
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //this.Close();
                Application.Exit();
            }

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Wednesday, December 16, 2009 1:50 AM
    Friday, December 11, 2009 7:26 AM