J.Doe
J.Doe

Reputation: 733

Application.Exit Isn't closing my program, any alternatives?

So in my form constructor I have a check for an argument. If I get one of two special arguments then I want to just register/unregister my server and close the program. I don't want the form to load in these instances. However as it currently stands the following code successfully registers/unregisters the server, but it doesn't kill my application right after like I want it to. Any other command to do so? Or maybe there's a better way to do what I'm trying to do?

My code:

    public Form1()
    {
        InitializeComponent();
        instance = this;
        fillMeasView();

        string[] args = Environment.GetCommandLineArgs();
        if (args.Length > 1)
        {
            switch (args[1])
            {
                case "register":
                    try
                    {
                        slikServer1.RegisterServer();
                        MessageBox.Show("Server registered successfully!");
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show("Error: Could not register server." + "\nAdditional Information: " + ex.Message, "Registering Server Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    Application.Exit();
                    break;
                case "unregister":
                    try
                    {
                        slikServer1.UnregisterServer();
                        MessageBox.Show("Server unregistered successfully!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not unregister server." + "\nAdditional Information: " + ex.Message, "Unregistering Server Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    Application.Exit();
                    break;
                default:
                    MessageBox.Show("This is the cmd line arg:" + args[1]);
                    break;
            }
        }

    }

Upvotes: 1

Views: 3122

Answers (3)

Luaan
Luaan

Reputation: 63732

You don't really want this code in the form constructor. Instead, find your entry point (usually a static Main method in the Program.cs file), and put it there.

Application.Exit is already a bit of a bad sign - it shows that you don't really know what you're trying to exit at that point. The reason it doesn't work in your case is that there's no application to exit yet - the usual way a winforms application is started looks something like this:

Application.Run(new Form1());

Since you're in the form constructor, Application.Run didn't run yet, so there's no message loop to exit (which is what Application.Exit does).

You'll also have direct access to command-line arguments from the entry point - it's an argument to the Main method. Do all your decisions there, and only if you want to actually run the GUI application, do the Application.Run. Otherwise, just return from Main and your application will end (provided you didn't spin up any foreground threads that are still alive at that point).

Upvotes: 0

egonr
egonr

Reputation: 982

You can use Environment.Exit(0). Nonzero Exit-Codes indicate an error.

Terminates this process and gives the underlying operating system the specified exit code. This is the code to call when you are using console application.

The difference to Application.Exit() becomes clear when looking at the documentation.

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

But be careful with Environment.Exit() since it kills the process immediatly. You may want to investigate why Application.Exit() is not able to close your application.

Upvotes: 0

ThePerplexedOne
ThePerplexedOne

Reputation: 2950

You want:

Environment.Exit(0);

Upvotes: 5

Related Questions