Process.Kill()没有';不要扼杀这个过程

本文关键字:过程 Kill 没有 Process | 更新日期: 2024-10-23 07:32:20

我正在创建一个新进程来执行一些长期运行的操作(pdf文件转换)。问题是,如果我想用它的ID杀死那个进程,它没有被杀死,我仍然在系统进程的列表上看到它。为什么?

using (Process p = new Process())
{
    p.StartInfo.FileName = "some_file_name";
    p.StartInfo.WorkingDirectory = "some_dir";
    p.StartInfo.Arguments = fullFilePath;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();
    myProcessID = p.Id;
    result.OutputMsg = p.StandardOutput.ReadToEnd(); <-- here it waits until operation completes
    result.ErrorMsg = p.StandardError.ReadToEnd();
    p.WaitForExit();
}
...
Process p = Process.GetProcessById(myProcessID);
if (p != null)
   p.Kill();

编辑

好吧,我看到它被杀死了,但转换仍在继续。我看到一个名为conhost.exe(控制台窗口主机)的新进程也在创建中,但没有它的ID。没有它,我无法删除它

Process.Kill()没有';不要扼杀这个过程

从MSDN上进程。杀死

Kill方法异步执行。调用Kill方法后,调用WaitForExit方法以等待进程退出,或者检查HasExited属性以确定进程是否已退出。

因此,它可能会因为某种原因而失败。如果你做了这个建议,你可以检查