Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit code of a process terminated with Process.Kill() , in C#

If in my C# application, I am creating a child process that can either terminate normally, or start misbehaving, in which case I terminate it with a call to Process.Kill().However, I would like to know if the process has exited normally.I know I can get the error code of a terminated process, but what would be a normal exit code and what would signify that the process was killed?

like image 780
Emil D Avatar asked Mar 16 '10 14:03

Emil D


People also ask

What is the exit code of a killed process?

Exit code 137 is Linux-specific and means that your process was killed by a signal, namely SIGKILL . The main reason for a process getting killed by SIGKILL on Linux (unless you do it yourself) is running out of memory.

What is process exit code?

Use ExitCode to get the status that the system process returned when it exited. You can use the exit code much like an integer return value from a main() procedure. The ExitCode value for a process reflects the specific convention implemented by the application developer for that process.

How do you terminate a process in C#?

Kill() Immediately stops the associated process.

Is exit an code?

What Does Exit Code Mean? An exit code or exit status is a number that is returned by an executable to show whether it was successful. This is also sometimes called a return code, or in some cases, an error code, although the terminology here may be slightly different.


2 Answers

The source code for Process.Kill() in shows setting the exit code to -1 (as of Sep 2012). The Windows operating system actually uses 32-bit unsigned values for the exit codes but I believe the signed->unsigned->signed translations in C# should work (I only need to check for zero or non-zero so it doesn't matter for my code).

On the other hand, this is obviously undocumented behavior (Why?) so Microsoft could change it tomorrow and break my code by setting the exit code to 0.

like image 119
mheyman Avatar answered Sep 28 '22 01:09

mheyman


You can use the property ExitCode but unfortunately this depends on how the app is built. So if you have the source code for the app make sure that it returns correctly upon a good exit. Please see this link to msdn describing this more in detail: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx

like image 39
Oskar Kjellin Avatar answered Sep 28 '22 02:09

Oskar Kjellin