C# 实时读取cmd输出

C# 实时读取cmd输出,c#,cmd,C#,Cmd,我正在编写一个程序,读取python脚本输出并在文本框中显示结果。 由于脚本运行了很长一段时间,我希望能够每1秒(或每行写入后)看到一次输出。 现在我只能在进程结束时看到输出。 有人知道问题出在哪里吗 我的代码片段: Process p = new Process(); p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.OutputDataReceived += ne

我正在编写一个程序,读取python脚本输出并在文本框中显示结果。 由于脚本运行了很长一段时间,我希望能够每1秒(或每行写入后)看到一次输出。 现在我只能在进程结束时看到输出。 有人知道问题出在哪里吗

我的代码片段:

Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.OutputDataReceived += new DataReceivedEventHandler (p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler (p_ErrorDataReceived);
p.Exited += new EventHandler (p_Exited);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "python.exe";
p.StartInfo.Arguments = "path " + commandline; 
p.Start(); 
StreamReader s = p.StandardOutput;
String output = s.ReadToEnd();
textBox3.Text = output;
p.WaitForExit();

我在自己的程序中采用以下方式:

private static void startProgram(
    string commandLine )
{
    var fileName = commandLine;
    var arguments = string.Empty;
    checkSplitFileName( ref fileName, ref arguments );

    var info = new ProcessStartInfo();
    info.FileName = fileName;
    info.Arguments = arguments;

    info.UseShellExecute = false;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;

    using ( var p = new Process() )
    {
        p.StartInfo = info;
        p.EnableRaisingEvents = true;

        p.OutputDataReceived += (s,o) => { 
            Console.WriteLine(o.Data);
        };
        p.Start();

        p.BeginOutputReadLine();

        p.WaitForExit();
    }
}
也就是说,我正在订阅并拨打电话。另见


(上述源代码中的方法
checkSplitFileName

我在自己的程序中采用以下方法:

private static void startProgram(
    string commandLine )
{
    var fileName = commandLine;
    var arguments = string.Empty;
    checkSplitFileName( ref fileName, ref arguments );

    var info = new ProcessStartInfo();
    info.FileName = fileName;
    info.Arguments = arguments;

    info.UseShellExecute = false;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;

    using ( var p = new Process() )
    {
        p.StartInfo = info;
        p.EnableRaisingEvents = true;

        p.OutputDataReceived += (s,o) => { 
            Console.WriteLine(o.Data);
        };
        p.Start();

        p.BeginOutputReadLine();

        p.WaitForExit();
    }
}
也就是说,我正在订阅并拨打电话。另见


(我上面的源代码中的方法
checkSplitFileName

我在从C#运行Python脚本时遇到同样的问题。问题是Python缓冲了来自
stdout
(print())的输出

你可以在这里做两件事中的一件

1. 在每行
print()
之后,将以下内容添加到Python脚本中,以刷新输出

import sys
print('Hello World!')
sys.stdout.flush()
2. 使用
-u
命令行参数运行Python编译器。这样,您不需要在每次打印后添加上面的刷新行

...
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "python.exe -u path " + commandline; 
...

我在从C#运行Python脚本时遇到了同样的问题。问题是Python缓冲了来自
stdout
(print())的输出

你可以在这里做两件事中的一件

1. 在每行
print()
之后,将以下内容添加到Python脚本中,以刷新输出

import sys
print('Hello World!')
sys.stdout.flush()
2. 使用
-u
命令行参数运行Python编译器。这样,您不需要在每次打印后添加上面的刷新行

...
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "python.exe -u path " + commandline; 
...

默认情况下,Python缓冲其输出。 方法是将“-u”命令行参数传递给python

因此,如果要执行say hello.py,可以执行以下操作:

python.exe-u hello.py

这是适合我的C代码

Process p = new Process();
string op = "";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "c:\\python27\\python.exe";
StreamReader outputStream = p.StandardOutput;
StreamReader errorStream = p.StandardError;
p.StartInfo.Arguments = @"-u hello.py";
p.Start();
string output = "";
int offset = 0, readBytes = 0;
char[] buffer = new char[512];
do
{
    output = outputStream.ReadLine();
    if (!string.IsNullOrEmpty(output))
    {
        txtOutput.AppendText(output);
        txtOutput.AppendText(Environment.NewLine);
        offset += readBytes;
        Application.DoEvents();
    }
    Thread.Sleep(3);
} while (!p.HasExited);

默认情况下,Python缓冲其输出。 方法是将“-u”命令行参数传递给python

因此,如果要执行say hello.py,可以执行以下操作:

python.exe-u hello.py

这是适合我的C代码

Process p = new Process();
string op = "";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "c:\\python27\\python.exe";
StreamReader outputStream = p.StandardOutput;
StreamReader errorStream = p.StandardError;
p.StartInfo.Arguments = @"-u hello.py";
p.Start();
string output = "";
int offset = 0, readBytes = 0;
char[] buffer = new char[512];
do
{
    output = outputStream.ReadLine();
    if (!string.IsNullOrEmpty(output))
    {
        txtOutput.AppendText(output);
        txtOutput.AppendText(Environment.NewLine);
        offset += readBytes;
        Application.DoEvents();
    }
    Thread.Sleep(3);
} while (!p.HasExited);