Exit console application in C #

I read a lot of things here about stackoverflow, comments, opinions and all ideas. But in any case, I really need an explained way to exit my console application (I'm on VS2010 Framework 4) in C # with a user error.

The best I could read right now is VB:

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

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

So my questions are:

  • Same as Environment.Exit (1)?
  • What is better for an application that works as an automatic task?
  • What do the exit codes -1, 0, 1 mean and what are their differences?
  • Regarding static void ExitProcess (uint uExitCode) ;?

Some earlier questions noted as bibliography:

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

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

+7
source share
6 answers

You can:

  • Calling .Exit (int) environment
  • Reclaim the main function with an int return and return the value from it.
+17
source

1) Environment.Exit is almost the same

2) I suppose the window automation service? try, but I think I won’t work as expected ...

3) see Windows Exitcodes

4) This is good, but you can use the built-in .Net method as well

+3
source

Environment.Exit will do the same as ExitProcess (plus, possibly, some .Net cleanup) - the end results are the same.

As for exit codes, they mean everything you want (reasonably) - 0 is “successful”,! = 0 “not successful”, but you can use any value other than 0 that you would like to mean to you like, it's up to a program using this value to do something useful with it.

+2
source

Below are some of the ExitCodes that apply to one of your questions 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 information about ExitCodes with detailed information, see here http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

+2
source

-one. Same as Environment.Exit (1)?

This is .NET, you barely knew which WINAPI they implemented for it without looking inside with windbg. However, nothing is documented about the similarities in ExitProcess and Environment.Exit . Environment.Exit may contain more than ExitProcess.

-2. What is better for an application that works as an automatic task?

If you are doing an automatic task using some kind of scheduler, then I also have another idea of ​​using Windows services. This will eliminate any headache of exit exit.

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

These are just codes to indicate the OS as a way out. You may have seen programmers pointing return 1 to error or return 0 to success. This allows you to use any of them.

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

You can use unmanaged calls in your application, and of course you will skip management activities with Environment.Exit .

0
source

I answer the question "What do the exit codes -1, 0, 1 mean and what are their differences?"

Various exit codes are just a way to transfer status with recipient applications. For example, in one of the projects that I involved, we used code 99 as a warning code. As long as all involved applications know what to do when they get 99 or something else, that's fine. In this application, if the caller receives something else, then 0 or 99, the caller must abort, and in case 99 he will continue.

0
source

All Articles