C#进程RedirectStandardOutput未重定向

本文关键字:重定向 RedirectStandardOutput 进程 | 更新日期: 2023-09-27 18:28:21

我想重定向richTextBoxProcess的标准输出。这是我的流程配置,

        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
        ...

我如何解决这个问题?

C#进程RedirectStandardOutput未重定向

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

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