9

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

Community
  • 1
  • 1
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 1
    What do you need explained that isn't explained in the post you linked to? And where is `static void ExitProcess(uint uExitCode)` defined? – Dan Puzey Aug 29 '12 at 14:42
  • Because I found some pages that says different things, and has others errors-code. I could not found a msdn helpful page, only on C++, like this one: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx By the way my code is defined on the class Program where resides the static void Main – Leandro Bardelli Aug 29 '12 at 14:46
  • 1
    Just return an int from `Main`, that is your exit code. – SomeWritesReserved Aug 29 '12 at 14:49
  • You maybe have found pages that say different things, but there *is* an answer to your question on StackOverflow already, and you should expect that that answer is "the answer" from StackOverflow. What about the existing answer do you not understand? – Dan Puzey Aug 29 '12 at 14:49
  • possible duplicate of [What is the command for exit an Console application on C#?](http://stackoverflow.com/questions/10286056/what-is-the-command-for-exit-an-console-application-on-c) – Dan Puzey Aug 29 '12 at 14:50
  • I saw the return solution but I deprecated it, it's sure for the environment? – Leandro Bardelli Aug 29 '12 at 14:50
  • @DanPuzey well first of all, the question has not an accepted answer. By the more voted solution, I'm not cleared if the return solution is clear and sure for the environment. The environment.exit is deprecated by the same answer – Leandro Bardelli Aug 29 '12 at 14:54

6 Answers6

21

You can:

  1. Call Environment.Exit(int)
  2. Re-declare your Main function as returning int, and return the value from it.
AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
3

1) Environment.Exit is nearly the same,

2) I guess the windows automation service? try it but i guess i won't work as expected...

3) look at this, Windows Exitcodes

4) It's nice but you could use the .Net built in method also

codeteq
  • 1,502
  • 7
  • 13
2

Environment.Exit will be doing the same thing as ExitProcess (plus perhaps some .Net cleaning up) - the end results are the same.

As for exit codes, they mean whatever you want them to (within reason) - 0 is 'successful', != 0 is 'not successful' but you can use any non-0 value you like to mean whatever you like, it's up to the program using that value to do something useful with it.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • uhm, I understand now why the 0 never changes but the others one yes, i saw a lot of examples with different values – Leandro Bardelli Aug 29 '12 at 14:47
  • re "They mean whatever you want them too" - Thats a bad thought. You are running on windows then use the windows codes. Thats what engineers expect to get. If you want an app that nobody understands the error code to then do whatever the hell you want. – Dan Dec 22 '16 at 22:47
2

Below are some of the ExitCodes specific to one of your question when using Environment.Exit(ExitCode)

ERROR_SUCCESS
0 (0x0)
The operation completed successfully.
ERROR_INVALID_FUNCTION
1 (0x1)
Incorrect function.
ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified.

For more ExitCodes with details please see here http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

HatSoft
  • 11,077
  • 3
  • 28
  • 43
1

-1. Is the same than Environment.Exit(1) ?

This is .NET, you barely come to know which WINAPI they have implemented for it without peeping inside with windbg.However nothing has been documented about the similarity in ExitProcess and Environment.Exit.Environment.Exit may contain more than ExitProcess.

-2. What is the better for an application that runs as automatic job?

If you a running automated job using some scheduler than I have also other idea of using windows services instead.This will remove any headache of exitprocess.

-3. What means the exit codes, -1, 0, 1, and what are their differences?

They are just codes to tell OS the kind of exit. You may have seen programmers specifying return 1 on error or return 0 on success.This makes you free to use any one of them.

-4. What about static void ExitProcess(uint uExitCode); ?

You are free to use unmanaged calls in your application and certainly you'll miss the management activity from Environment.Exit.

perilbrain
  • 7,961
  • 2
  • 27
  • 35
0

I am answering the question "What means the exit codes, -1, 0, 1, and what are their differences?"

Different exit codes are just a method of communicating status with caller applications. For example in one of the projects I involved, we used 99 as a warning code. As long as all the applications involved knows what to do when they receive 99 or something else it is fine. In that application if the caller receive anything else then 0 or 99, the caller should abort and in the case of 99 it was free to continue.

ebayindir
  • 441
  • 3
  • 10
  • What do you do when your application fails to start and windows returns error code 99 for the reason? You fail to execute properly. Please stop thinking these #'s mean nothing. It may well be that you are returning jibberish, but that doesn't mean that people trying to use your app are always getting errors from you. Its best to conform to the Error messages found here, there are plenty of space for your own custom messages.: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx – Dan Dec 22 '16 at 22:51