What is the command in C# for exiting a console application?
-
2Does this answer your question? How do I specify the exit code of a console application in .NET?– Michael FreidgeimCommented Nov 15, 2021 at 6:23
-
The canonical is How do I specify the exit code of a console application in .NET?.– Peter MortensenCommented Jan 17, 2022 at 22:42
-
For exiting by Trace.Assert() (usually conditional) and its exit code (134 on some platforms), see this.– Peter MortensenCommented Jan 18, 2022 at 23:21
4 Answers
You can use Environment.Exit(0);
and Application.Exit
Environment.Exit(0)
is cleaner.
-
13
-
This worked perfectly for me when I added a timer to a console app via stackoverflow.com/a/7865126/1530166. Application.Exit didn't do it, even when using stackoverflow.com/a/37029044/1530166. Commented Oct 12, 2017 at 16:18
-
7@GrantH. it is cleaner because it gives the underlying operating system a more useful error code why it is exiting. Commented Nov 8, 2017 at 8:23
-
However,
Environment.Exit
requires that you have SecurityPermissionFlag.UnmanagedCode permissions - which might be troublesome for some.– wp78deCommented Sep 11, 2018 at 20:47
Several options, by order of most appropriate way:
- Return an int from the Program.Main method
- Throw an exception and don't handle it anywhere (use for unexpected error situations)
- To force termination elsewhere,
System.Environment.Exit
(not portable! see below)
Edited 9/2013 to improve readability
Returning with a specific exit code: As Servy points out in the comments, you can declare Main with an int
return type and return an error code that way. So there really is no need to use Environment.Exit unless you need to terminate with an exit code and can't possibly do it in the Main method. Most probably you can avoid that by throwing an exception, and returning an error code in Main if any unhandled exception propagates there. If the application is multi-threaded you'll probably need even more boilerplate to properly terminate with an exit code so you may be better off just calling Environment.Exit.
Another point against using Evironment.Exit
- even when writing multi-threaded applications - is reusability. If you ever want to reuse your code in an environment that makes Environment.Exit
irrelevant (such as a library that may be used in a web server), the code will not be portable. The best solution still is, in my opinion, to always use exceptions and/or return values that represent that the method reached some error/finish state. That way, you can always use the same code in any .NET environment, and in any type of application. If you are writing specifically an app that needs to return an exit code or to terminate in a way similar to what Environment.Exit
does, you can then go ahead and wrap the thread at the highest level and handle the errors/exceptions as needed.
-
14That last option is in bad taste. No programmer want to exit a program by throwing exception. Commented Apr 23, 2012 at 18:28
-
6As I wrote "by order of most appropriate". Besides, in certain situations throwing an exception IS the correct way (such as when an unexpected, fatal error that shouldn't happen occurs from which the application will never recover). I've updated the answer to clarify.– sinelawCommented Apr 23, 2012 at 18:33
-
If you're in Main you can just return 0, 1, etc. and mark the method signature as returning an int to return an error code, you don't NEED to use Environment.Exit to specify an error code.– ServyCommented Apr 23, 2012 at 18:46
-
3Depends on how deep in the call stack you are and whether you're in the main thread. It can be simpler to call Environment.Exit than to architect your entire program to return all the way back to the main function without using an exception to unwind the call stack (which wouldn't work from a background thread; either the ThreadPool would swallow the exception or would throw it out unhandled, either way any catch logic in the main method or thread would never see it.– KeithSCommented Apr 23, 2012 at 18:49
-
1I changed the order of the options. Throwing an exception has the advantage of making your code much easier to reuse as a library in different executable environments.– sinelawCommented Jun 26, 2013 at 18:45
Console applications will exit when the main function has finished running. A "return" will achieve this.
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("I'm running!");
return; //This will exit the console application's running thread
}
}
If you're returning an error code you can do it this way, which is accessible from functions outside of the initial thread:
System.Environment.Exit(-1);
-
-
A negative value indicates an error to the calling process. This signals to the calling application that it was an error and not a successful run. Returning 0 or a positive integer indicates success. tutorialspoint.com/batch_script/batch_script_return_code.htm Commented Jul 13, 2022 at 18:34
You can use Environment.Exit(0)
and Application.Exit
.
Environment.Exit()
: terminates this process and gives the underlying operating system the specified exit code.