Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi guys,

as i'm coding for learning the three methods, i found an interesting thing:

The programm is:
C#
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
                 System::Windows::Forms::DialogResult result1;
                 result1= MessageBox::Show("sure to quit?", "", MessageBoxButtons::YesNo, MessageBoxIcon::Question);
                 if(result1==System::Windows::Forms::DialogResult::Yes)
                 {
                     Application::Exit();
                     //this->Close();  //has the same result
                     //Application::ExitThread();  //programm ends, but i'm wondering if all threads are ended     
                 }
              }
private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
                 System::Windows::Forms::DialogResult result2;
                 result2= MessageBox::Show("sure to quit?", "", MessageBoxButtons::YesNo, MessageBoxIcon::Question);
                 if(result2==System::Windows::Forms::DialogResult::Yes)
                 {
                    e->Cancel=false;
           //Application::Exit(); //twice click so that it closes                  
                 }
                 else
                 {e->Cancel=true;}
             }


Also, what i wanna my prog to do is:
1. when button2 is clicked, show a messagebox to ask the user if he really wants to quit.
2. when "X" is clicked, show a messagebox to ask the user if he really wants to quit.

Problem:
You need to click twice "yes" to leave the form by situation1(button2). But if i cancel the situation 2, the "Application::Exit()" works well.


Why this happens? Could you experts explain? I don't need msdn links for Exit(), ExitThread() and Close() because i've read them more than 10 times. Maybe i didn't understand them. If so, please explain where is the point.
Thanks a lot!
Posted
Comments
[no name] 29-Aug-12 9:46am    
It's not the Exit(), ExitThread() or Close that you need to understand in this situation. You need to understand the events. What do you think happens when you click button 2? You call exit right? What happens when your application exits? The form closing event fires. What code is it that you have duplicated?
christmars 29-Aug-12 10:03am    
so you mean the closing event and the application exit method is duplicated? or what? as i thought, the Exit() just breaks all the events down and the FormClosing is just an event that schould also be included, right?
[no name] 29-Aug-12 10:09am    
I mean exactly what I said. Your form closing event is called as a result of your calling Exit in your click event. Your button 2 click event is mostly unneccessary as you are just duplicating code. Just call Exit and let the form closing event do the check.
pasztorpisti 29-Aug-12 20:17pm    
"+5"
[no name] 29-Aug-12 21:43pm    
Thank you

1 solution

This is unrelated to ExitThread and not directly related to Application.Exit. Before you exit your application, you really need to have all your other thread exited, unless they are background threads or the threads obtained via the ThreadPool. I would advice to take care about explicit ending of all non-UI thread, in case you need some control on the order of operation.

Now, the confirmation from the user is not related to all that. Here is what you really need:

On the main form, handle the event FormClosing or override the virtual method OnFormClosing:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx[^].

This event allows you to cancel the close operation. For this purpose, set System.Windows.Forms.FormClosingEventArgs.Cancel to true:
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx[^].

You can if the check if closing was requested by the user: check the property FormClosingEventArgs.CloseReason:
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[^].

If this is UserClosing and some other conditions are met (for example, some unsaved document or unfinished processing could need the user confirmation) you can request the user for confirmation with some dialog box and see what the user chooses.

—SA
 
Share this answer
 
v3
Comments
H.Brydon 28-Jun-13 22:28pm    
SA, this is just one of many detailed questions you provide (one of many +5 from me by the way). I'm just curious - Do you have a library of answers hidden somewhere or do you hit Google for every one of your solutions?
Sergey Alexandrovich Kryukov 28-Jun-13 22:32pm    
Thank you, Harvey.

This very answer is a brand new one. But of course, I have such a "library", but this is nothing but a set of links to some of my previous answers, + some more links interesting links and sayings. And, by the way, I keep all my records related to CodeProject and my publications in Revision Control System, presently this is Subversion.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900