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 ?
Explanation. EXIT_SUCCESS. successful execution of a program. EXIT_FAILURE. unsuccessful execution of a program.
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.
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.
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.
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
PropertyProcess.StandardInput
PropertyProcess.StandardOutput
PropertyIf you are using non-Windows environment, you should check the related reference.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With