Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of EXIT_SUCCESS and EXIT_FAILURE in C#?

I'm starting a process and want to check its exit code for success or failure.

Process myProcess = new Process();

myProcess.Start();

myProcess.WaitForExit();

// What should i put instead of EXIT_SUCCESS ?
if (myProcess.ExitCode == EXIT_SUCCESS) 
{
  // Do something
}

EXIT_SUCCESS doesn't seem to exist, is there an equivalent or is the canonical way in C# to just check against zero ?

like image 670
Drax Avatar asked Jan 23 '15 12:01

Drax


People also ask

What does EXIT_FAILURE mean in C?

Explanation. EXIT_SUCCESS. successful execution of a program. EXIT_FAILURE. unsuccessful execution of a program.

What does exit EXIT_FAILURE do in C?

If the value of status is EXIT_FAILURE , an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

What does EXIT_SUCCESS mean?

The value EXIT_SUCCESS is defined in stdlib. h on ecelinux as 0 and is used to indicate success; other integer values can be returned to indicate some form of failure, although EXIT_FAILURE is defined as 1 in stdlib.

What does exit EXIT_SUCCESS do?

The EXIT_SUCCESS and EXIT_FAILURE macros expand into integral expressions that can be used as arguments to the std::exit function (and, therefore, as the values to return from the main function), and indicate program execution status.


2 Answers

You can find the System Error Codes reference on MSDN:

ERROR_SUCCESS
0 (0x0) The operation completed successfully.

As it's the only one code for success and other for error, the section is called Error codes. This isn't specific for C#, only for Windows.

Also note that you can scan the standard input/output/error streams for the proccess:

  • Process.StandardError Property
  • Process.StandardInput Property
  • Process.StandardOutput Property

If you are using non-Windows environment, you should check the related reference.

like image 63
VMAtm Avatar answered Nov 15 '22 03:11

VMAtm


From C/C++ code normally those are defined as constants in some include header that makes use of those. So you typically have something like this:

#define EXIT_SUCCESS 0

And similar definitions for some other possible return codes. After all, those "exit codes" are just numeric values that are defined as constants by some include file, but not a language construct. As C# lacks includes and header files, you may want to define them manually:

private const int EXIT_SUCCESS = 0

And then just use in your code as any other constant. So your sample code will now compile and work as expected

if (myProcess.ExitCode == EXIT_SUCCESS) 
{
  // Do something
}
like image 21
Alejandro Avatar answered Nov 15 '22 03:11

Alejandro