1

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;
            }
        }

    }
J.Doe
  • 713
  • 1
  • 6
  • 19
  • 5
    Move that code to `Main` method (application entry point) and don't show form at all instead of exiting anything. – Sinatr Nov 30 '16 at 15:04
  • @Sinatr you're saying to create a main? That'll add ambiguity in terms of the programs start point won't it? – J.Doe Nov 30 '16 at 15:13
  • You already have it... somewhere ;) Do not create new, use/modify existing one. There is even special version of it: see [this answer](http://stackoverflow.com/a/1179545/1997232). – Sinatr Nov 30 '16 at 15:14
  • 1
    @J.Doe Put the checks in the program start point (`main`). Then only load the form if needed – bixarrio Nov 30 '16 at 15:16

3 Answers3

5

You want:

Environment.Exit(0);
ThePerplexedOne
  • 2,920
  • 15
  • 30
  • what does that command do exactly? I'll give it a try now thanks – J.Doe Nov 30 '16 at 15:04
  • @J.Doe it kills the process and assign ExitCode = 0 – Steve Nov 30 '16 at 15:05
  • "Terminates this process and returns an exit code to the operating system." - [MSDN](https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx) – ThePerplexedOne Nov 30 '16 at 15:05
  • Nope still not working for me, form1 still seems to load. EDIT: forgot to apply it to register server command. Only did unregister. Works fine now thanks sir : ) – J.Doe Nov 30 '16 at 15:11
0

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.

sknt
  • 963
  • 6
  • 16
0

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).

Luaan
  • 62,244
  • 7
  • 97
  • 116