C# 调用python子进程并重定向
1 /* 2 * 由SharpDevelop创建。 3 * 用户: Administrator 4 * 日期: 2021/10/15 5 * 时间: 19:16 6 * 7 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件 8 */ 9 //主窗体添加 richTextBox1 richTextBox2 Button1 Button2 控件,,粘贴运行 10 using System; 11 using System.Collections.Generic; 12 using System.Drawing; 13 using System.Windows.Forms; 14 15 using System.Diagnostics; 16 using System.IO; 17 18 namespace SharpCPython_Idle 19 { 20 /// <summary> 21 /// Description of MainForm. 22 /// </summary> 23 public partial class MainForm : Form 24 { 25 System.Diagnostics.Process pyProcess=new Process(); 26 string pyExePh=@"X:\...\python.exe"; 27 ProcessStartInfo pyInfo; 28 29 StreamWriter pyStmW ; 30 31 public MainForm() 32 { 33 34 InitializeComponent(); 35 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; 36 37 pyInfo=pyProcess.StartInfo; 38 pyInfo.FileName=pyExePh; 39 pyInfo.Arguments="-i"; 40 pyInfo.UseShellExecute=false; 41 pyInfo.CreateNoWindow=true; 42 43 pyInfo.RedirectStandardInput=true; 44 pyInfo.RedirectStandardOutput=true; 45 pyInfo.RedirectStandardError=true; 46 47 pyProcess.OutputDataReceived+= new DataReceivedEventHandler(pyProcess_OutputDataReceived); 48 pyProcess.ErrorDataReceived+= new DataReceivedEventHandler(pyProcess_ErrorDataReceived); 49 50 pyProcess.Start(); 51 52 pyStmW =pyProcess.StandardInput; 53 54 pyProcess.BeginOutputReadLine(); 55 pyProcess.BeginErrorReadLine(); 56 57 58 pyStmW.WriteLine(@"print('\nhello world\n')"); 59 60 61 } 62 63 void pyProcess_OutputDataReceived( Object sender , DataReceivedEventArgs e) 64 { 65 richTextBox2.SelectionColor=Color.Black; 66 richTextBox2.Text+=e.Data; 67 Console.Write( e.Data ); 68 69 } 70 void pyProcess_ErrorDataReceived( Object sender , DataReceivedEventArgs e) 71 { 72 73 Color tmpClr=richTextBox2.SelectionColor; 74 int i=richTextBox2.Text.Length; 75 76 richTextBox2.SelectionColor=Color.Red; 77 richTextBox2.Text+='\n'+e.Data+'\n'; 78 richTextBox2.Select(i,e.Data.Length+1); 79 richTextBox2.DeselectAll(); 80 richTextBox2.SelectionColor=tmpClr; 81 82 } 83 84 void Button1Click(object sender, EventArgs e) 85 { 86 pyStmW.Write(richTextBox1.Text); 87 pyStmW.WriteLine("\n"); 88 89 } 90 91 void Button2Click(object sender, EventArgs e) 92 { 93 richTextBox2.Text=""; 94 } 95 } 96 }
/*
* 由SharpDevelop创建。
* 用户: Administrator
* 日期: 2021/10/15
* 时间: 19:16
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace SharpCPython_Idle
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
System.Diagnostics.Process pyProcess=new Process();
string pyExePh=@"D:\Program_Files[LvSe]\blender-2.79b-windows32\blender-2.79b-windows32\2.79\python\bin\python.exe";
ProcessStartInfo pyInfo;
StreamWriter pyStmW ;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
pyInfo=pyProcess.StartInfo;
pyInfo.FileName=pyExePh;
pyInfo.Arguments="-i";
pyInfo.UseShellExecute=false;
pyInfo.CreateNoWindow=true;
pyInfo.RedirectStandardInput=true;
pyInfo.RedirectStandardOutput=true;
pyInfo.RedirectStandardError=true;
pyProcess.OutputDataReceived+= new DataReceivedEventHandler(pyProcess_OutputDataReceived);
pyProcess.ErrorDataReceived+= new DataReceivedEventHandler(pyProcess_ErrorDataReceived);
pyProcess.Start();
pyStmW =pyProcess.StandardInput;
// pyStmR=pyProcess.StandardOutput;
pyProcess.BeginOutputReadLine();
pyProcess.BeginErrorReadLine();
pyStmW.WriteLine(@"print('\nhello world\n')");
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void pyProcess_OutputDataReceived( Object sender , DataReceivedEventArgs e)
{
richTextBox2.SelectionColor=Color.Black;
richTextBox2.Text+=e.Data;
Console.Write( e.Data );
}
void pyProcess_ErrorDataReceived( Object sender , DataReceivedEventArgs e)
{
Color tmpClr=richTextBox2.SelectionColor;
int i=richTextBox2.Text.Length;
richTextBox2.SelectionColor=Color.Red;
richTextBox2.Text+='\n'+e.Data+'\n';
richTextBox2.Select(i,e.Data.Length+1);
richTextBox2.DeselectAll();
richTextBox2.SelectionColor=tmpClr;
}
void Button1Click(object sender, EventArgs e)
{
pyStmW.Write(richTextBox1.Text);
pyStmW.WriteLine("\n");
}
void Button2Click(object sender, EventArgs e)
{
richTextBox2.Text="";
}
}
}
浙公网安备 33010602011771号