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 7071385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:40:53+00:00 2026-05-28T05:40:53+00:00

I’m having trouble understanding how to close an C# .NET winforms application. What I’m

  • 0

I’m having trouble understanding how to close an C# .NET winforms application. What I’m trying to do is:

Display a form to allow the user to set-up the environment how they want
If the user presses the “OK” button, perform some logic for setting up the application environment (instantiate objects, etc)
If the user presses “Cancel” or closes the window, close the application.

The problem is, I’m calling the environment set-up form before the main (1st) form. It’s a recent requirement change, and I didn’t fancy re-writing the code that I have from the very beginning.

The code I have (which should make more sense than my little preamble) is:

    public MainForm()
    {
        InitializeComponent();
        //Get the user to set-up the environment (load specific config files, etc)

        environmentSetupForm newEnvrionmenSetupForm = new environmentSetupForm ();
        if (newEnvrionmenSetupForm .ShowDialog() == DialogResult.OK)
        {
            newEnvrionmenSetupForm .Close();
            //some logic based on what the user selected on the set-up form
        }
        else
        {
            //Since the user didn't set-up the environment correctly (there
            //was a message box, informing them and giving them another
            //shot at it), exit out of the application.
            Application.Exit();
        }
    }

My only problem is that after Application.Exit(), the stack jumps back to Program.cs and performs

        Application.Run(new MainForm());

So the Main form (and app) runs regardless. Is there a better way to do what I’m trying to do?

Edit: for clarity my program.cs code reads:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Windows.Forms;

   namespace myNamespace
   {
      static class Program
      {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
        static void Main()
        {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new MainForm());
        }
     }
   }
  • 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-28T05:40:53+00:00Added an answer on May 28, 2026 at 5:40 am

    Neither the form’s constructor nor its OnLoad or Load event are good places to put this code. The constructor runs due to the new MainForm() statement in the Main() method, before the Application.Run() call. The Load event is fired because the Application class calls the Show() method, hidden inside the framework code just before Application.Run() enters the message loop. Application.Exit() cannot do anything until that message loop starts running.

    The workaround is to move this code to the Main() method in Program.cs. Make it look similar to this:

        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainForm main;            
            using (var dlg = new environmentSetupForm()) {
                if (dlg.ShowDialog() != DialogResult.OK) return;
                // Use dlg values
                //...
                main = new MainForm();
                // Make main form show up at the same location
                main.StartPosition = FormStartPosition.Manual;
                main.Location = dlg.Location;
            }
            Application.Run(main);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the

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.