What is the command to quit the Console Application program? I am using a method to display a final message and then from there I would like to quit the whole program.
Pls help, thanks.
What is the command to quit the Console Application program? I am using a method to display a final message and then from there I would like to quit the whole program.
Pls help, thanks.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Aug 6, 2007, 4:38 AM
The argument to Environment.Exit is just a way of returning a value to the operating system and is placed in the environment variable ERRORLEVEL.
You can always find out what value was returned by typing the following at the command prompt just after your console application has exited:
echo %ERRORLEVEL%
However, this variable is typically used in batch processing (i.e. .bat files) when it can be tested using the IF ERRORLEVEL construct and the appropriate action then taken.
If an application ends normally, then it is conventional to return a value of 0 to the OS which is the default if you don't set it to anything else. Where an application is ended abruply, some programmers (including myself) prefer to use a value of 1 instead but, if you're not doing anything with the value, then it really doesn't matter what value you return as Jan said.
Incidentally, another way to return a value to the OS when your Main() method ends is to use one of the overloads which has a return type of 'int' rather than 'void'.
Jan MontanoPosted Aug 6, 2007, 3:36 AM
Exit Codes or Errorlevels Set by MS-DOS Commands
KeithPosted Aug 5, 2007, 11:46 PM
Thanks Alan.
My lecturer gave me Environment.Exit(0); instead.
What is the difference between a 0 and 1 in the argument ?
Mike GoldPosted Aug 5, 2007, 5:13 PM
Actually Alan's method is what you were probably looking for.
Environment.Exit(1) will abruptly terminate your program.
Mike GoldPosted Aug 5, 2007, 5:10 PM
You just need to return from the main method provided. In fact all console apps will quit unless you put a Console.ReadLine (or some infinite loop) at the end of the main method to keep it from quitting.
Another words, the program below quits immediately after running the Main method. If you are stuck somewhere simply use return until you've come back to the main method and return.
namespace
ConsoleApplication2{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello");
}
}
}
AlanPosted Aug 5, 2007, 5:08 PM