简体   繁体   中英

Process.Kill() doesn't kill the process

I'm creating a new process to do some long-running action (pdf file conversion). The problem is, if I want to kill that process using its ID, it's not killed, I still see it on the system process' list. Why ?

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

EDIT

OK, I see it's being killed, but the the conversion still continues. I see that a new process is also being created with name conhost.exe (console window host), but don't have its ID. Without it I cannot delete it

From the MSDN on Process.Kill

The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.

So, it might be failing for some reason. If you do the suggestion, you can check that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM