Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1032181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:02:32+00:00 2026-05-16T14:02:32+00:00

Basically I have a main form which upon loading, opens a child form for

  • 0

Basically I have a main form which upon loading, opens a child form for logging in the user. When they cancel or close this login form, I need to close the whole application.

But there seems to be a few different ways to close a C# program:

  1. Application.Exit();

  2. Application.ExitThread();

  3. Environment.Exit(1);

  4. Process.GetCurrentProcess().Kill();

  5. SFTPClient.LDAPLoggedIn = false; Close();

EDIT: Sorry if this one is not clear: It sets a property in a controller object to indicate that the login failed. After opening the child form, I would check this property in the parent form to see whether the program should continue or not. It basically shifts the responsibility of exiting the program to the parent without an exception.

6: throw new Exception("User closed the form");

I can see that there are two ways of handling it:

  • Informing the parent that something went wrong (as in 5 and 6.)
  • Closing the program from the child form.

Is either of these two considered better practice?

Each approach seems to have the same effect on my program but how do they actually compare?

UPDATE:
Thanks for the answers. For those searching this question in the future and curious people, this was my solution in the end:

private void FormMain_Load(object sender, EventArgs e)
{
    if (new FormLogin().ShowDialog(this) == DialogResult.Cancel) Close();
}

and:

private void buttonCancel_Click(object sender, EventArgs e)
{
    Close();
}

I discovered that when a form is closed via clicking the ‘X’, DialogResult is set to Cancel automatically so all I need to do is Close()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:02:32+00:00Added an answer on May 16, 2026 at 2:02 pm

    If you are wanting to gracefully handle the exception in the last case, that’s ok (not great though) – as long is it is an exceptional situation to your application. Otherwise I’d create a new method that shows the form as a dialog with a boolean. If the boolean comes back false (aka, user closed the form) I would then handle the application shut down from there (Using Application.Exit()).

    It is, in my humble opinion, very bad practice to close the application from a child rather than telling the parent. The only time I agree with this is in a FailFast situation, which are very rare.


    • Application.Exit();

    This method stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

    • Application.ExitThread();

    See above.


    • Environment.Exit(1);

    Terminates this process and gives the underlying operating system the specified exit code.


    • Process.GetCurrentProcess().Kill();

    Kill forces a termination of the process, while CloseMainWindow only requests a termination. When a process with a graphical interface is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system.


    • SFTPClient.LDAPLoggedIn = false; Close();

    After the clarification in the comment (passes flow back to the parent and handles from there), this is by far the best way of doing this.


    • throw new Exception("User closed the form");

    Throws an exception to the calling process. If this is the main thread, it will throw the exception in a very ugly way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main class(which is basically a netbeans form;drag and drop) from where
lets say i have a main form which have a lot of functionallity. this
I need help with the following design. Basically I have a Main form which
Basically, I have somewhere in my main code where this line of code is
I have a web application which basically process entries of the user when the
I have created a UserControl using a DevExpress XtraTreeList. Basically in this user control
The title pretty much says it all, but basically I have a main parent
I basically have three tables, posts, images and postimages (this simply contains the ids
I have created an application which inserts data into a SQL database. Basically, from
So basically, I have a form with a Calendar(frmCalendar) control on it. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.