首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RedirectStandardOutput正在缓冲线路而不是瞬间?

RedirectStandardOutput正在缓冲线路而不是瞬间?
EN

Stack Overflow用户
提问于 2008-10-26 11:46:47
回答 2查看 2.1K关注 0票数 0

好的,我正在尝试使用尾来监视日志文件,但是我不能像使用相同的参数在cmd提示符中手动运行它时那样,编程地获得相同的行为。

当运行cmd提示符时,它会立即显示新行。不过,从程序上讲,在“缓冲区”释放所有行之前,我必须等待日志文件中的75+新行

这是我现在的密码。

代码语言:javascript
复制
private const string tailExecutable = @"C:\tail.exe";
private const string logFile = @"C:\test.log";

private static void ReadStdOut()
{
    var psi = new ProcessStartInfo
    {
        FileName = tailExecutable,
        Arguments = String.Format("-f \"{0}\"", logFile),
        UseShellExecute = false,
        RedirectStandardOutput = true
    };

    // Running same exe -args through cmd.exe 
    // works perfectly, but not programmatically.
    Console.WriteLine("{0} {1}", psi.FileName, psi.Arguments);

    var tail = new Process();
    tail.StartInfo = psi;
    tail.OutputDataReceived += tail_OutputDataReceived;
    tail.Start();
    tail.BeginOutputReadLine();
}

static void tail_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}

我以前使用过OutputDataReceived事件,但从未遇到过这些缓冲/垃圾邮件问题。

我现在很困惑。

*编辑*

我找到了这个在CodeProject上的wintail项目,并且将切换到它,因为缓冲区使这个解决方案太慢了。

谢谢你的回答。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-10-26 12:30:28

当重定向时,Process.StandardOutput默认为带有4096字节缓冲区的StreamReader,因此答案是肯定的。

票数 3
EN

Stack Overflow用户

发布于 2008-10-26 12:27:48

在大多数语言和操作系统中,标准流通常是缓冲的,但错误流不是。

尝试使用:System.Console.Error

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/237914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档