Why Does TaskList.exe Never End When Redirecting Standard Output?

随声附和 提交于 2019-12-25 08:05:27

问题


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

If I set RedirectStandardOutput to false, the process does end!!!

Why does the tasklist.exe Process never end? I am using Windows 7 and .net framework 4.5.2.

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?


回答1:


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


来源:https://stackoverflow.com/questions/41273181/why-does-tasklist-exe-never-end-when-redirecting-standard-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!