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();
}