C#的System.Diagnostics.Process重定向标准输出的大量数据 [英] C# System.Diagnostics.Process redirecting Standard Out for large amounts of data

查看:846
本文介绍了C#的System.Diagnostics.Process重定向标准输出的大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑从.NET应用程序的exe文件,并试图重定向标准输出到一个StreamReader。问题是,当我做

I running an exe from a .NET app and trying to redirect standard out to a streamreader. The problem is that when I do

myprocess.exe >> out.txt

myprocess.exe >> out.txt

out.txt接近14MB。 当我这样做的命令行版本,它是非常快的,但是当我从我的CSHARP应用程序运行的过程中,它是速度奇慢因为我相信默认StreamReader的刷新每4096个字节。

out.txt is close to 14mb. When I do the command line version it is very fast but when I run the process from my csharp app it is excruciatingly slow because I believe the default streamreader flushes every 4096 bytes.

有没有办法更改默认流读取器的进程对象?

Is there a way to change the default stream reader for the Process object?

推荐答案

我还没有尝试过,但它看起来像异步方法可以提供更好的性能。相反 process.StandardOutput 的使用,试试这个方法来代替:

I haven't tried, but it looks like the asynchronous methods may offer better performance. Instead of using process.StandardOutput, try this method instead:

Process process = Process
    .Start(new ProcessStartInfo("a.exe"){RedirectStandardOutput = true});
if (process != null)
{
    process.OutputDataReceived += ((sender, e) =>
                                   {
                                       string consoleLine = e.Data;
                                       //handle data
                                   });
    process.BeginOutputReadLine();
}

这篇关于C#的System.Diagnostics.Process重定向标准输出的大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆