Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ .NET
Hi guys,
 
as i'm coding for learning the three methods, i found an interesting thing:
 
The programm is:
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 29-Aug-12 1:20am
Comments
Wes Aday at 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 at 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?
Wes Aday at 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 at 29-Aug-12 20:17pm
   
"+5"
Wes Aday at 29-Aug-12 21:43pm
   
Thank you
Sergey Alexandrovich Kryukov at 28-Jun-13 17:44pm
   
You are right. I provided a correct recipe on what should be done; please see my answer.
—SA
Wes Aday at 29-Aug-12 21:45pm
   
Just wanted you to know that these are exactly the kind of questions that I appreciate. You had an actual question, you tried to do something and you clearly explained what it was that you wanted to do. So I up voted this one.
christmars at 30-Aug-12 1:53am
   
thank you!! now i understand what exactly you mean. i see it when i made a little change of the messageboxes, when they are different, the logic is clear!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v3
Comments
H.Brydon at 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 at 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)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 28 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100