Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#WinForm中嵌入交互式python进程 问题:_C#_Python_Winforms_Python 3.x_Process - Fatal编程技术网

如何在C#WinForm中嵌入交互式python进程 问题:

如何在C#WinForm中嵌入交互式python进程 问题:,c#,python,winforms,python-3.x,process,C#,Python,Winforms,Python 3.x,Process,我试图将Python作为一个System.Diagnostics.Process启动,并将其标准输出、输入和错误重定向到Windows窗体上的RichTextBox,以便在窗体上有一个Python REPL 进程正确启动(Start()返回true),但从未从进程发送任何输出或错误数据: 如果我试图从标准输出或标准错误读取,读取将挂起 如果我改为订阅OutputDataReceived和ErrorDataReceived它们将不会触发 我只在通过发送Ctrl+C(在asd行的末尾按Ente

我试图将Python作为一个
System.Diagnostics.Process
启动,并将其标准输出、输入和错误重定向到Windows窗体上的
RichTextBox
,以便在窗体上有一个Python REPL

进程正确启动(
Start()
返回
true
),但从未从进程发送任何输出或错误数据:

  • 如果我试图从
    标准输出
    标准错误
    读取,读取将挂起
  • 如果我改为订阅
    OutputDataReceived
    ErrorDataReceived
    它们将不会触发

我只在通过发送
Ctrl+C
(在
asd
行的末尾按
Enter
后发送,因此是第三行)终止python后才看到它的输出

为了澄清这一点,我希望在输入“1+1”后按enter键后,看到python(从读取StandardOutput流或在数据事件中)发送的“2”

为什么在python执行时没有数据发送到标准输出,也没有引发事件?是否有可能按照我的要求完成这项工作(以便立即给出python的响应)。

附加的 如果我改为启动
cmd.exe
,那么一切都很好,我可以像使用常规命令提示符一样使用表单。所以在某种程度上,我的申请是有效的

但是,如果我尝试使用此表单启动python,它将挂起,直到我像以前一样通过发送
Ctrl+C
终止进程:

显然,标准的输入是python(因为python错误消息报告的行号根据前面的行数而变化)

我正在使用: win32上的Python 3.5.2 | Anaconda 4.2.0(64位)|(默认值,2016年7月5日,11:41:13)[MSC v.1900 64位(AMD64)]

再次: 为什么python不将任何数据发送到standard out,或者导致任何事件被引发,直到它死掉?我该怎么做?

代码 其中一些遵循了www.codeproject.com/Articles/335909/embedded-a-Console-in-a-C-Application的模式

安装程序
RichTextBox的按键事件
写输出
单击“Ctrl+C”按钮的侦听器发送
Ctrl+C
就是这样

inputWriter.WriteLine("\x3");
基于这个问题,

// some extra code here I haven't included to prevent deletion of stuff printed from standard out
// just checks the cursor position and then suppresses the event if certain keys are pressed
// I can provide if it will help

if (e.KeyCode == Keys.Return)
{
    string input = textConsole.Text.Substring(inputStart, textConsole.SelectionStart - inputStart);
    lastInput = input;
    inputStart = textConsole.SelectionStart + 1; // inluding the new line
    inputWriter.WriteLine(input);
    inputWriter.Flush();
    if (string.IsNullOrWhiteSpace(input)) e.SuppressKeyPress = true;
}
public void WriteOutput(string output, Color color)
{
    if (string.IsNullOrEmpty(lastInput) == false &&
        (output == lastInput || output.Replace("\r\n", "") == lastInput))
        return;

    Invoke((Action)(() =>
    {
        //  Write the output.
        textConsole.SelectionColor = color;
        textConsole.AppendText(output);
        inputStart = textConsole.SelectionStart;
    }));
}
inputWriter.WriteLine("\x3");