Exit from a console application in C#

Leandro picture Leandro · Aug 29, 2012 · Viewed 45.6k times · Source

I read a lot of things here on stackoverflow, comments, opinions and every kind of ideas. But anyway, I really need an explained way to exit my console application (I'm on VS2010 Framework 4) on C# with a custom error.

The best thing I could read right now is on VB:

Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

and use ExitProcess(1) for an error or ExitProcess(0)

So my questions are:

  • Is the same than Environment.Exit(1) ?
  • What is the better for an application that runs as automatic job?
  • What means the exit codes, -1, 0, 1, and what are their differences?
  • What about static void ExitProcess(uint uExitCode); ?

Some previously questions marked as bibliography:

What is the command to exit a Console application in C#?

http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

Answer

AnthonyLambert picture AnthonyLambert · Aug 29, 2012

You can:

  1. Call Environment.Exit(int)
  2. Re-declare your Main function as returning int, and return the value from it.