There are two ways of doing it.
First one is very limited and based on exit code. On Windows and Unix-like systems, all applications return integer code which defaults to 0. Traditionally, 0 means "everything is OK", but it could be anything. With Python, exit code is returned by assigning an integer value to the special variable
$?
. Please see:
http://stackoverflow.com/questions/285289/exit-codes-in-python[
^].
On the side of your .NET parent process, when the child process is terminated, you can pick up the exit code using the property
System.Diagnostics.Process.ExitCode
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx[
^].
You can synchronize a thread of your parent process with the termination of your child process using the methods
System.Diagnostics.Process.WaitForExit
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[
^].
Please note that these methods are blocking, so they are not suitable for calling, say, from a UI thread — you might need to execute all your code using
System.Diagnostics.Process
from a separate thread, at least in the case if your Python script takes considerable time to complete.
The second approach is more universal but a bit more awkward and is based on
console output. You can echo some output text to the console in your Python script. Your parent process needs to pick up this text, parse and process it the way you need. To do that, you need to redirect output from the child process (Python script, in this case) to a stream and read the content of this stream. This is done by redirection of
StandardOutput
, and, just in case,
StandardError
streams. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx[
^].
Both MSDN help pages referenced above show how to do the redirection on the code samples. Please see — they are descriptive enough.
—SA