问题描述
当我尝试使用Process类运行tasklist.exe并将RedirectStandardOutput设置为true时,该过程永远不会结束.
When I attempt to run tasklist.exe with the Process class and set RedirectStandardOutput to true, the process never ends.
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
RunProcess("tasklist.exe");
}
private static void RunProcess(string command)
{
var process = new Process()
{
StartInfo =
{
FileName = command,
RedirectStandardOutput = true,
UseShellExecute = false
}
};
process.Start();
process.WaitForExit();
}
}
如果我将RedirectStandardOutput设置为false,则过程结束!!!
If I set RedirectStandardOutput to false, the process does end!!!
为什么tasklist.exe进程永不结束?我正在使用Windows 7和.net Framework 4.5.2.
Why does the tasklist.exe Process never end? I am using Windows 7 and .net framework 4.5.2.
我发现,当我强制关闭tasklist.exe时,每次都会有4096个字节写入标准输出!我需要增加某种字符缓冲区的大小吗?
I found out that when I forcefully close tasklist.exe, there is exactly 4096 bytes written to standard output every time! Is there some kind of character buffer that I need to increase in size?
推荐答案
如果您使用的是RedirectStandardOutput = true
,请在代码中添加以下行:
If you're using RedirectStandardOutput = true
add this line to your code:
process.Start();
// To avoid deadlocks, always read the output stream first and then wait.
string out = process.StandardOutput.ReadToEnd();
process.WaitForExit();
这篇关于为什么重定向标准输出时TaskList.exe永不结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!