Reputation: 522
I am making an application with visual c# in the front end and Python executing the scripts in the back end. I wanted to pass one value from Visual c# form as an argument to a Python script. The Python script should process the value and should return the processed value to Visual c#. The value should be displayed in the visual c# form.
For the first thing, I wrote a dirty code where python script is executed when the form is loaded and store the value in a text file. Python script will use the value to calculate. But I am not able to return the value to c#.
The code which I wrote for the first logic is:
Process process = new Process();
process.StartInfo.CreateNoWindow = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.FileName = @"C:\c#\Work\RulesValidator\RulesValidator\Asset_Id.py";
try
{
process.Start();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
asset_id.Text = System.IO.File.ReadAllText(@"C:\c#\Work\RulesValidator\RulesValidator\Asset_Id.txt");
Upvotes: 2
Views: 2095
Reputation: 148900
The problem is that you start the child Python script and immediately try to read the sequential file without waiting for the end of the child.
You should try :
Process process = new Process();
process.StartInfo.CreateNoWindow = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.FileName = @"C:\c#\Work\RulesValidator\RulesValidator\Asset_Id.py";
try
{
process.Start();
process.WaitForExit(); // Now child should have done its job and closed file
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
asset_id.Text = System.IO.File.ReadAllText(@"C:\c#\Work\RulesValidator\RulesValidator\Asset_Id.txt");
But you should investigate the way proposed by e-nouri. The only attention point is : To use StandardOutput, you must set ProcessStartInfo.UseShellExecute to false. On the Python part, you would only write the result to stdout, every other output going to stderr.
It could be (adapted from this page on MSDN) :
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"C:\c#\Path\To\python.exe";
p.StartInfo.Arguments = @"C:\c#\Work\RulesValidator\RulesValidator\Asset_Id.py";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
BEWARE : I've no C# developpement environment and above is untested even if I honestly think it should not be far from what you want
Upvotes: 1
Reputation: 2626
You can spawn a process and capture its stdout, here is an SO question for this: link. In your python program/script you can just use prints that will print to the stdout, then capture it from your C# code. Depends the complexity of that data you want to exchange, you can use plain text, json, etc.
Cheers,
Upvotes: 1