C# - 无法终止CreateNoWindow设置为true的进程

时间:2013-09-05 20:06:07

标签: c# process cmd

我正在尝试与手刹的cli进行交互,并且在需要时我遇到了终止它的过程的问题。

流程就像这样开始

        string command = "/c \"C:\\Program Files\\Handbrake\\HandBrakeCLI.exe\" -i " + disk.Info.Name + " -o " + disk.Info.VolumeLabel + ".mkv -t " + titleNum + " -e x264 -q 18.0 -f mkv -4 --decomb --loose-anamorphic --modulus 2 -m --x264-preset medium --h264-profile high --h264-level 4.1";

        _p = new Process();
        _p.StartInfo = new ProcessStartInfo("cmd.exe", command)
        {
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardError = true,
            RedirectStandardOutput = true
        };

        _p.OutputDataReceived += OutputCallback;
        _p.ErrorDataReceived += OutputCallback;

        _p.Start();

        _p.BeginErrorReadLine();
        _p.BeginOutputReadLine();

        _p.WaitForExit();

我尝试使用_p.Kill()_p.Close()_p.Dispose()_p.CloseMainWindow()来终止它。最后一个有效但仅在CreateNoWindow = false

1 个答案:

答案 0 :(得分:0)

问题是你正在启动shell并告诉它启动Handbrake; _p指的是shell进程。杀死它显然对手刹没有影响。

你应该直接启动Handbrake:

string command = " -i " + disk.Info.Name + " -o " + disk.Info.VolumeLabel + ".mkv -t " + titleNum + " -e x264 -q 18.0 -f mkv -4 --decomb --loose-anamorphic --modulus 2 -m --x264-preset medium --h264-profile high --h264-level 4.1";

_p = new Process();
_p.StartInfo = new ProcessStartInfo("C:\\Program Files\\Handbrake\\HandBrakeCLI.exe\", command)
{
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardOutput = true
};