How do I specify the exit code of a console application in .NET?
Three options:
- You can return it from
Main
if you declare yourMain
method to returnint
. - You can call
Environment.Exit(code)
. - You can set the exit code using properties:
Environment.ExitCode = -1;
. This will be used if nothing else sets the return code or uses one of the other options above).
Depending on your application (console, service, web application, etc.), different methods can be used.
Exit from a console application in C#
You can:
- Call Environment.Exit(int)
- Re-declare your Main function as returning int, and return the value from it.
How implement exit from console application using ctrl + x?
The CTRL+C and CTRL+BREAK key combinations receive special handling by console processes. By default, when a console window has the keyboard focus, CTRL+C or CTRL+BREAK is treated as a signal (SIGINT or SIGBREAK) and not as keyboard input. By default, these signals are passed to all console processes that are attached to the console. (Detached processes are not affected. See Creation of a Console.) The system creates a new thread in each client process to handle the event. The thread raises an exception if the process is being debugged. The debugger can handle the exception or continue with the exception unhandled.
CTRL+BREAK is always treated as a signal, but an application can change the default CTRL+C behavior in two ways that prevent the handler functions from being called:
- The SetConsoleMode function can disable the ENABLE_PROCESSED_INPUT input mode for a console's input buffer, so CTRL+C is reported as keyboard input rather than as a signal.
- When SetConsoleCtrlHandler is called with NULL and TRUE values for its parameters, the calling process ignores CTRL+C signals. Normal CTRL+C processing is restored by calling SetConsoleCtrlHandler with NULL and FALSE values. This attribute of ignoring or not ignoring CTRL+C signals is inherited by child processes, but it can be enabled or disabled by any process without affecting existing processes.
So we can do some basic things and disable CTRL+C and CTRL+Break first.
Console.TreatControlCAsInput = false;
Console.CancelKeyPress += delegate (object? sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
};
Console.WriteLine("Hello, World!");
Console.ReadKey();
The next step is hook up and add ctrl+x
Console.TreatControlCAsInput = false;
Console.CancelKeyPress += delegate (object? sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
};
ConsoleKeyInfo cki;
do
{
Console.WriteLine("Hello, World!");
//todo any work here
cki = Console.ReadKey(true);
} while (cki.Modifiers != ConsoleModifiers.Control && cki.Key != ConsoleKey.X);
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.
Related Topics
C# - Multiple Generic Types in One List
Making a Simple Ajax Call to Controller in ASP.NET MVC
C# Switch Statement Limitations - Why
Open Existing File, Append a Single Line
Reading 64Bit Registry from a 32Bit Application
Differencebetween a Shared Project and a Class Library in Visual Studio 2015
Error: "Cannot Modify the Return Value" C#
C# Reflection: How to Get Class Reference from String
How to Delete a Line from a Text File in C#
Entering Keys Manually with Entity Framework
Why Does My .Net 4 Application Know .Net 4 Is Not Installed
C# Equivalent of SQL Server Datatypes
How to Generate and Validate a Software License Key
How to Get the Name of the Current Method from Code
Difference Between Invariantculture and Ordinal String Comparison
How to Convert a String to Its Equivalent Linq Expression Tree
Using Protobuf-Net, I Suddenly Got an Exception About an Unknown Wire-Type