Following are the ways by which we can exit an application:
Environment.Exit(0)Application.Exit()Form.Close()
What is the difference between these three methods and when to use each one?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The proper method would be
Application.Exit(). According to the Documentation, it terminates all message loops and closes all windows thus giving your forms the possibility to execute their cleanup code (in Form.OnClose etc).Environment.Exitwould just kill the process. If some form has e.g. unsaved changes it would not have any chances to ask the user if he wants to save them. Also resources (database connections etc.) could not be released properly, files might not be flushed etc.Form.Closejust does what it says: it closes a form. If you have other forms opened (perhaps not now but in some future version of your application), the application will not terminate.Keep in mind that if you use multithreading,
Application.Exit()will not terminate your threads (and thus the application will keep working in the background, even if the GUI is terminated). Therefore you must take measures to kill your threads, either in the main function (i.e.Program.Main()) or when in theOnCloseevent of your main form.