Reputation: 522
I am using Visual C# as UI and Python in the background.
Is this possible?
Upvotes: 0
Views: 1520
Reputation: 2289
You could start your python in a new process in the background and pass the form items as arguments in the process start information as if you were running the python script from the command line, like so:
var start = new ProcessStartInfo
{
FileName = _pathToPythonExecutable,
Arguments = string.Format(" {0} --arg1 {1}",_pathToYourPythonScript, //formItemValue),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
WorkingDirectory = _currentWorkingDirectory
};
using (Process process = Process.Start(start))
{
// Do stuff
}
You'll see that in my start information, I have told the process to Redirect StandardInput, Standard Output and Standard Error. This is another way that you can pass data between the processes.
You would write to standard input like so:
process.StandardInput.Write(input);
process.StandardInput.Close();
Standard output and Standard Errors are streams, so you can read them like so
// This would be the same for standard error
using (StreamReader reader = process.StandardOutput)
{
result = reader.ReadToEnd();
}
Upvotes: 2