Command to close an application of console?
Environment.Exit
and Application.Exit
Environment.Exit(0)
is cleaner.
http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
What is the command to exit a console application in C#?
You can use Environment.Exit(0);
and Application.Exit
Environment.Exit(0)
is cleaner.
To exit the console window in a C# console application
Environment.Exit
and Application.Exit
Environment.Exit(0)
is cleaner.
Console Application: Closing Window vs End of Program
A console mode program is a pretty hostile place for the COM interop wrapper objects that are needed for an apartment-threaded out-of-process COM server. This program demonstrates the issue:
using System;
class Program {
static void Main(string[] args) {
var prg = new Program();
Console.ReadLine();
}
~Program() {
Console.WriteLine("Clean-up completed");
System.Threading.Thread.Sleep(1500);
}
}
Try it both ways, by pressing Enter and by clicking the Close button. You'll see that the finalizer never gets executed. The operating system terminates the process before it gets a chance to shut down properly when you click the Close button.
Same problem with the finalizers for the COM wrappers. They cannot execute so IUnknown::Release() doesn't get called and the Office program is completely unaware that the client program is no longer there. Windows has its own cleanup for abandoned out-of-process servers but that doesn't work for Office programs for some otherwise mysterious reason.
That explains it, fixing it isn't so easy. You'll have to register a callback that runs when the Close button is clicked. If necessary, set the app reference to null if it is still in scope and force the finalizer to run with GC.Collect() + GC.WaitForPendingFinalizers(). Do keep in mind that this is just a band-aid, not a fix. It won't work when the user aborts your program while you are busy talking to the Office program. Avoiding a console mode project is best.
On Exit for a Console Application
You need to hook to console exit event and not your process.
http://geekswithblogs.net/mrnat/archive/2004/09/23/11594.aspx
Capture console exit C#
Why is the console window closing immediately once displayed my output?
the issue here is that their Hello World Program is showing up then it would immediately close.
why is that?
Because it's finished. When console applications have completed executing and return from their main
method, the associated console window automatically closes. This is expected behavior.
If you want to keep it open for debugging purposes, you'll need to instruct the computer to wait for a key press before ending the app and closing the window.
The Console.ReadLine
method is one way of doing that. Adding this line to the end of your code (just before the return
statement) will cause the application to wait for you to press a key before exiting.
Alternatively, you could start the application without the debugger attached by pressing Ctrl+F5 from within the Visual Studio environment, but this has the obvious disadvantage of preventing you from using the debugging features, which you probably want at your disposal when writing an application.
The best compromise is probably to call the Console.ReadLine
method only when debugging the application by wrapping it in a preprocessor directive. Something like:
#if DEBUG
Console.WriteLine("Press enter to close...");
Console.ReadLine();
#endif
You might also want the window to stay open if an uncaught exception was thrown. To do that you can put the Console.ReadLine();
in a finally
block:
#if DEBUG
try
{
//...
}
finally
{
Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
#endif
Terminating a c# Console application
Use a loop and just let Main return normally. Furthermore, I also tried to simplify the condition checking a bit along with the string comparison and parsing. Your error message suggests a validation range ("between" 0 and 100) that is not actually enforced by the preceding if/else if logic. For instance your first case (... <= 50) would be true if user enters a negative value. Also, I did not see where price was ever declared so I made up a constant in my example.
static bool ExitRequired(string line)
{
return string.Equals(line, "q", StringComparison.OrdinalIgnoreCase);
}
static void Main(string[] args)
{
const double price = 10;
int userInputDigit;
double userCost;
string line = null;
while (!ExitRequired(line))
{
Console.WriteLine("Enter a number or press 'q' to exit...");
line = Console.ReadLine();
if (ExitRequired(line))
break;
if (int.TryParse(line, out userInputDigit)
&& userInputDigit > 0
&& userInputDigit < 100)
{
if (userInputDigit <= 50)
{
userCost = (price * userInputDigit);
Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
}
else if ((userInputDigit > 50) && (userInputDigit <= 80))
{
userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
}
else if ((userInputDigit > 80) && (userInputDigit <= 100))
{
userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
}
}
else
{
Console.WriteLine("Error! Please input a number between 0 and 100");
}
}
}
Related Topics
What Is Going on with 'Gets(Stdin)' on the Site Coderbyte
Which Std::Async Implementations Use Thread Pools
Is #Pragma Once Part of the C++11 Standard
Common Reasons for Bugs in Release Version Not Present in Debug Mode
Why Is "!=" Used with Iterators Instead of "<"
Allocconsole() Not Displaying Cout
How to Write C++ Getters and Setters
What Is the Underlying Data Structure of a Stl Set in C++
Lvalue to Rvalue Implicit Conversion
Replacing Ld with Gold - Any Experience
Will Casting Around Sockaddr_Storage and Sockaddr_In Break Strict Aliasing
C++ Filehandling: Difference Between iOS::App and iOS::Ate
Are C++ Templates Just MACros in Disguise
How to Give Priority to Privileged Thread in Mutex Locking