我要解析一个大的文件,所以不要做的:
string unparsedFile = myStreamReader.ReadToEnd(); // takes 4 seconds
parse(unparsedFile); // takes another 4 seconds
我要带的前4秒的优势,并尝试做一些喜欢做在同一时间两件事:
while (true)
{
char[] buffer = new char[1024];
var charsRead = sr.Read(buffer, 0, buffer.Length);
if (charsRead < 1)
break;
if (charsRead != 1024)
{
Console.Write("Here"); // debuger stops here several times why?
}
addChunkToQueue(buffer);
}
这里是debuger的图像:(我加int counter
上显示我们读什么迭代小于1024个字节)
请注意,那里有643个字符阅读,而不是1024在接下来的迭代,我得到:
我想我应该读1024个字节的所有时间,直到我最后一次迭代,其中remeining字节小于1024。
所以我的问题是 ,为什么会读字符的“随机”数我扔迭代while循环?
编辑
我不知道什么样的流我处理。 我喜欢执行的过程:
ProcessStartInfo psi = new ProcessStartInfo("someExe.exe")
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
// execute command and return ouput of command
using (var proc = new Process())
{
proc.StartInfo = psi;
proc.Start();
var output = proc.StandardOutput; // <------------- this is where I get the strem
//if (string.IsNullOrEmpty(output))
//output = proc.StandardError.ReadToEnd();
return output;
}
}