首页 文章

C#进程RedirectStandardOutput没有重定向

提问于
浏览
0

我想在 richTextBox 中重定向 Process 的standardoutput . 这是我的流程配置,

string command = "/K perl C:\\Server.pl ";

        ProcessStartInfo startInfo = new ProcessStartInfo();
        Process proc = new Process();
        startInfo.WindowStyle = ProcessWindowStyle.Normal;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = command;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;

        proc.StartInfo = startInfo;
        proc.OutputDataReceived += (s, ea) => this.richTextBox1.AppendText(ea.Data);
        proc.Start();
        proc.BeginOutputReadLine();

这是我的 Server.pl 文件

print "Server1 \n";

        while(1)
        {
            print "Server \n";
            sleep 1;
        }

但是当我运行程序时,cmd.exe只是黑色而没有在richTextBox中打印 . 但是当我改变了

startInfo.RedirectStandardOutput = false;

我把它放在我的cmd.exe中:

Server1
        Server
        Server
        Server
        ...

我如何解决这个问题?

1 回答

  • 2

    可能就像在perl脚本中禁用输出缓冲一样简单 . 这是使用 $| 特殊变量完成的(请参阅perlvar) .

    $| = 1;
    print "Server1 \n";
    ...
    

相关问题