C# C语言中的Python控制台#
我试图创建一个实现python控制台的控件。不幸的是,它没有收到任何Python输出C# C语言中的Python控制台#,c#,python,multithreading,process,C#,Python,Multithreading,Process,我试图创建一个实现python控制台的控件。不幸的是,它没有收到任何Python输出 Queue<string> qstring = new Queue<string>(); Thread thread; Process prc; void pythonmain() { if (DesignMode) return; prc = new Process(); prc.StartInfo
Queue<string> qstring = new Queue<string>();
Thread thread;
Process prc;
void pythonmain()
{
if (DesignMode) return;
prc = new Process();
prc.StartInfo.FileName = "python";
prc.StartInfo.Arguments = " -u";
prc.StartInfo.RedirectStandardInput = true;
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.RedirectStandardError = true;
prc.StartInfo.UseShellExecute = false;
prc.StartInfo.CreateNoWindow = true;
prc.EnableRaisingEvents = true;
prc.OutputDataReceived += new DataReceivedEventHandler(prc_OutputDataReceived);
prc.ErrorDataReceived += new DataReceivedEventHandler(prc_ErrorDataReceived);
prc.SynchronizingObject = this;
prc.Start();
prc.BeginOutputReadLine();
prc.BeginErrorReadLine();
while (!prc.HasExited)
{
lock (qstring)
{
if (qstring.Count > 0) prc.StandardInput.Write(qstring.Dequeue()+"\n");
}
}
prc.WaitForExit();
}
void prc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
richTextBox1.BeginInvoke(new Action<string>((s) => richTextBox1.AppendText(s)),e.Data);
}
}
void prc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
System.Diagnostics.Process p = (Process)sender;
if (e.Data != null)
{
richTextBox1.BeginInvoke(new Action<string>((s) => richTextBox1.AppendText(s)), e.Data);
}
}
public PyConsoleControl()
{
InitializeComponent();
if (!DesignMode)
{
thread = new Thread(pythonmain);
thread.IsBackground = true;
thread.Start();
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
lock (qstring)
{
qstring.Enqueue(textBox1.Text);
}
textBox1.Clear();
}
}
}
Queue qstring=new Queue();
螺纹;
处理中国;
void pythonmain()
{
如果(设计模式)返回;
prc=新工艺();
prc.StartInfo.FileName=“python”;
prc.StartInfo.Arguments=“-u”;
prc.StartInfo.RedirectStandardInput=true;
prc.StartInfo.RedirectStandardOutput=true;
prc.StartInfo.RedirectStandardError=true;
prc.StartInfo.UseShellExecute=false;
prc.StartInfo.CreateNoWindow=true;
prc.EnableRaisingEvents=真;
prc.OutputDataReceived+=新的DataReceivedEventHandler(prc\U OutputDataReceived);
prc.ErrorDataReceived+=新的DataReceivedEventHandler(prc\U ErrorDataReceived);
prc.SynchronizingObject=此;
prc.Start();
prc.BeginOutputReadLine();
prc.BeginErrorReadLine();
当(!prc.已退出)
{
锁(qstring)
{
如果(qstring.Count>0)prc.StandardInput.Write(qstring.Dequeue()+“\n”);
}
}
中华人民共和国;外国退出();
}
无效prc_ErrorDataReceived(对象发送方,DataReceivedEventArgs e)
{
如果(如数据!=null)
{
richTextBox1.BeginInvoke(新操作(=>richTextBox1.AppendText)),即数据);
}
}
无效prc_OutputDataReceived(对象发送方,DataReceivedEventArgs e)
{
System.Diagnostics.Process p=(进程)发送方;
如果(如数据!=null)
{
richTextBox1.BeginInvoke(新操作(=>richTextBox1.AppendText)),即数据);
}
}
公共PyConsoleControl()
{
初始化组件();
如果(!设计模式)
{
线程=新线程(pythonmain);
thread.IsBackground=true;
thread.Start();
}
}
私有void textBox1\u KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.KeyCode==Keys.Enter)
{
锁(qstring)
{
qstring.Enqueue(textBox1.Text);
}
textBox1.Clear();
}
}
}
您可以看到我正在创建一个新线程,该线程正在启动一个Python shell,但没有返回Python输出。我该如何修复它呢?当然可以看看,它几乎肯定会满足您的需求
比如说
using System;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
// we get access to Action and Func on .Net 2.0
using Microsoft.Scripting.Utils;
namespace TestCallIronPython
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
ScriptEngine pyEngine = Python.CreateEngine();
ScriptScope pyScope = pyEngine.CreateScope();
pyScope.SetVariable("test", "test me");
string code = @"
print 'test = ' + test
class MyClass:
def __init__(self):
pass
def somemethod(self):
print 'in some method'
def isodd(self, n):
return 1 == n % 2
";
ScriptSource source = pyEngine.CreateScriptSourceFromString(code);
CompiledCode compiled = source.Compile();
compiled.Execute(pyScope);
// Get the Python Class
object MyClass = pyEngine.Operations.Invoke(pyScope.GetVariable("MyClass"));
// Invoke a method of the class
pyEngine.Operations.InvokeMember(MyClass, "somemethod", new object[0]);
// create a callable function to 'somemethod'
Action SomeMethod2 = pyEngine.Operations.GetMember<Action>(MyClass, "somemethod");
SomeMethod2();
// create a callable function to 'isodd'
Func<int, bool> IsOdd = pyEngine.Operations.GetMember<Func<int, bool>>(MyClass, "isodd");
Console.WriteLine(IsOdd(1).ToString());
Console.WriteLine(IsOdd(2).ToString());
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
直接取自示例。请小心,您应该确保流程实际启动,为此,您应该检查方法启动的结果。如果进程启动,则返回true,否则返回false。是否考虑使用?