Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit from a console application in C#

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

like image 616
Leandro Bardelli Avatar asked Aug 29 '12 14:08

Leandro Bardelli


1 Answers

You can:

  1. Call Environment.Exit(int)
  2. Re-declare your Main function as returning int, and return the value from it.
like image 89
AnthonyLambert Avatar answered Sep 17 '22 17:09

AnthonyLambert