42

I'm maintaining a number of console applications at work and one thing I've been noticing in a number of them is that they call Environment.Exit(0).

A sample program would look like this:

public class Program
{
    public static void Main(string[] args)
    {
        DoStuff();
        Environment.Exit(0);
    }
}

I don't understand what the intent of the original programmer was in doing this? In my mind even without the Environment.Exit statement the program should exit just fine. That said, for one of these programs, it's console window has been remaining even after it was supposed to have closed so I'm really not sure what's going on there....

Am I missing something here? Or is there a reason why Environment.Exit should be called in this instance?

mezoid
  • 28,090
  • 37
  • 107
  • 148
  • 6
    a lot of this stuff is programmer superstition, like closing and disposing of connection objects, even though it's wrapped in a using – Matthew Lock Apr 19 '12 at 06:20
  • 1
    @MatthewLock Certain outside processes or scripts may check the exit code to see if the executable completed without any errors. That's why it's a good practice to return a 0 on exit. Like calling your command line application from NodeJS / Electron is one example of this. So it's not all programmer superstition. – HK1 Jul 21 '22 at 18:32

7 Answers7

62

The only reason for calling Exit() as the last line of the Main method is if there might be other foreground threads running. They would stay running if execution just fell off the end of Main. Even in this case, it would usually be a better idea either to put in some explicit graceful termination into the other threads - or make them background threads to start with.

If you ever want to return a different exit code from Main, the simpler way to achieve that is to declare it to return int.

In short, I don't think you need Environment.Exit() here, and it's worth asking your colleagues exactly why they're using it - chances are they won't be able to give you a good reason, and it's another bit of fluff you can cut out.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks jon! Don't think I'll have much luck asking the original developer about it since she is on maternaty leave. At least I now know that I can remove it without worrying about breaking something. – mezoid Mar 28 '09 at 12:07
  • The linked question http://stackoverflow.com/questions/5253158/c-using-environment-exitcode-versus-returning-int-from-main also added usefull info. – Bjørn Otto Vasbotten Sep 30 '14 at 07:25
16

Basically, the statement Environment.Exit(0) tells the operating system that this is a "clean" exit. There are other numbers as well, each with a different meaning like, Environment.Exit(1).

However, one thing to note is that the "Main" has been declared as returning nothing "void", so the exit code will really not have a meaning to it.

Just in case you wanted to know more about the different exit codes, have a look here:

System Error Codes (0-499)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
  • 5
    `Main` being declared as not returning anything has nothing to do with the meaning of the exit code. – Ry- Jul 24 '16 at 02:59
2

This is (compatibility) for command-line programs to indicate success or failure to an underlying shell, and is inherited from older C-style main loops where the prototype of the main function was

int main(void);

int main(int argc, char *argv[]);

The return value of 0 traditionally meant success, while non-zero meant failure or something else, depending on what the programmer decided.

References

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert Paulson
  • 17,603
  • 5
  • 34
  • 53
  • In my case, my console application is a stand alone application and doesn't need to tell the OS anything. So I guess that means the call is redundant in my situation....? – mezoid Mar 28 '09 at 06:32
  • It's more about telling the shell program if your program succeeded or failed, which may be important down the road, so go with the convention. e.g. batch files, nAnt integration, installers, etc. – Robert Paulson Mar 28 '09 at 06:40
  • 2
    Worth noting that a .NET console app can also have its main method return `int` as well as `void`. I do it all the time. – tomfanning Sep 27 '12 at 06:56
0

This is really used when other applications are waiting on the result of your console application. For example, SQL Server Reporting Services (SSRS) (tool) can launch a console application and waits to get back a success of failure response. The Environment.Exit(0) sends back a successful execution message.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yongil
  • 1
0

In SSIS, when you have an Execute Process Task and you want to know if process is failure, this method is useful.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Farshid
  • 1
  • 1
  • What do you mean by *"if process is failure"*? Do you mean *"if the process is a failure"*? Or "the process exited with a failure code" (different from 0?)? Or something else? – Peter Mortensen Jan 17 '22 at 22:20
0

In .NET Core, as of right now, one must use Environment.Exit to terminate their program on Mac. Purely returning from main doesn't stop the process and it keeps running until the user aborts it. Patterns like:

public class Program
{
    public static void Main(string[] args)
    {
        Environment.Exit(TryRun(args));
    }
}

are common in my work place because we want to ship for both Windows and Mac.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Czipperz
  • 3,268
  • 2
  • 18
  • 25
0

I'm currently using Environment.Exit() in a console application where I don't want to throw ugly exception text back to the console. Instead, I just notice the condition, write a user-friendly message to the screen about what happened and then call Environment.Exit().

Otherwise, you shouldn't need to call it from Main() in a basic console application.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101