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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Aug 5, 2007, 1:08 PM EDT
Mike GoldRank #50 · 30.6K reputationPosted Aug 5, 2007, 1:10 PM EDT
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");
}
}
}
Mike GoldRank #50 · 30.6K reputationPosted Aug 5, 2007, 1:13 PM EDT
Actually Alan's method is what you were probably looking for.
Environment.Exit(1) will abruptly terminate your program.
Keith35 reputationPosted Aug 5, 2007, 7:46 PM EDT
Thanks Alan.
My lecturer gave me Environment.Exit(0); instead.
What is the difference between a 0 and 1 in the argument ?
Jan MontanoRank #1,295 · 1.3K reputationPosted Aug 5, 2007, 11:36 PM EDT
Exit Codes or Errorlevels Set by MS-DOS Commands
AlanRank #669 · 3.6K reputationPosted Aug 6, 2007, 12:38 AM EDT
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'.