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
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
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.
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
-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
.
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.