So far I've written some similar stories about how C # and Python work together.
In "Run Python script from C # GUI application", Python script is executed and the progress can be checked until the process is completed.
In "Run Python code from C # GUI", I tried to make it possible to input Python code from the screen instead of the Python script.
And in "Run and manage processes in the background from C # GUI", after executing Python, do another work without waiting for the end. At the same time, I investigated how to check the execution status when I was interested.
Therefore, this is the last story in this series.
** In the first place, I want to create a method that executes a Python script and receives the processing result from a GUI, but what do you do? ** **
In short, it is a call that starts a Python script without going through the GUI, waits there (synchronizes) without going to the next process until the execution is completed, and then gets the result.
Yes, don't.
PythonExecutor.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace ChemShow
{
public class PythonExecutor
{
public String FileName { get; set; }
public String WorkingDirectory { get; set; }
public String Arguments { get; set; }
public String InputString { get; set; }
public String StandardOutput { get; set; }
public int ExitCode { get; set; }
private StringBuilder standardOutputStringBuilder = new StringBuilder();
public PythonExecutor()
{
}
///Behavior when the execute button is clicked
public void Execute()
{
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = this.FileName;
psInfo.WorkingDirectory = this.WorkingDirectory;
psInfo.Arguments = this.Arguments;
psInfo.CreateNoWindow = true;
psInfo.UseShellExecute = false;
psInfo.RedirectStandardInput = true;
psInfo.RedirectStandardOutput = true;
psInfo.RedirectStandardError = true;
// Process p = Process.Start(psInfo);
Process p = new System.Diagnostics.Process();
p.StartInfo = psInfo;
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;
//Process execution
p.Start();
//Write to standard input
using (StreamWriter sw = p.StandardInput)
{
sw.Write(InputString);
}
//Start reading output and error asynchronously
p.BeginOutputReadLine();
p.BeginErrorReadLine();
//Wait till the end
p.WaitForExit();
this.ExitCode = p.ExitCode;
this.StandardOutput = standardOutputStringBuilder.ToString();
}
/// <summary>
///Processing when standard output data is received
/// </summary>
void p_OutputDataReceived(object sender,
System.Diagnostics.DataReceivedEventArgs e)
{
//processMessage(sender, e);
if (e != null && e.Data != null && e.Data.Length > 0)
{
standardOutputStringBuilder.Append(e.Data + "\n");
}
}
/// <summary>
///What to do when a standard error is received
/// </summary>
void p_ErrorDataReceived(object sender,
System.Diagnostics.DataReceivedEventArgs e)
{
//Write the necessary processing
}
}
}
There's nothing new, just start the process and just run `WaitForExit ()`
. Then you won't be able to continue until the process is finished. As a result, synchronous calling can be realized. After the execution is completed, the exit code and standard output data can be obtained from the properties.
The following is an example of giving data to the standard input of a script and receiving the result output to the standard output.
PythonExecutor pe = new PythonExecutor();
pe.FileName=@"c:\python3.6\bin\python.exe";
pe.WorkingDirectory = @"c:\tmp";
pe.Arguments =@"C:\ChemShow\python\hist.py";
pe.InputString = "1,2,3,4,5,6,7,8,9,10"; //Data given to standard input
pe.Execute(); //Run
Console.WriteLine(pe.StandardOutput); //Output standard output result
Fixed as follows because there was a fatal bug that started the process twice. We apologize for the inconvenience.
// Process p = Process.Start(psInfo); Process p = new System.Diagnostics.Process(); p.StartInfo = psInfo;
In short, it's a story of making a component that corresponds to all the calling methods that have come out so far.
Later, the title is a Python script, but it's the same if it can be executed from the command line.
Recommended Posts