Click here to Skip to main content
11,924,800 members (56,869 online)
Rate this:
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);
                     //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);
           //Application::Exit(); //twice click so that it closes                  

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.

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
Wes Aday 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?
Wes Aday 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
Wes Aday 29-Aug-12 21:43pm
Thank you
Sergey Alexandrovich Kryukov 28-Jun-13 17:44pm
You are right. I provided a correct recipe on what should be done; please see my answer.
Wes Aday 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 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
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:[^],[^].

This event allows you to cancel the close operation. For this purpose, set System.Windows.Forms.FormClosingEventArgs.Cancel to true:[^],[^].

You can if the check if closing was requested by the user: check the property FormClosingEventArgs.CloseReason:[^],[^].

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.

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151125.3 | Last Updated 28 Jun 2013
Copyright © CodeProject, 1999-2015
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