简体   繁体   中英

Killing processes in c# Cannot process request because the process has exited [duplicate]

This question already has an answer here:

I am writing a program in c# that creates a process of google chrome in incognito mode. Everything goes good. I want to start the process and after 2 seconds to kill it(and close the chrome window).

 String a = textBox1.Text + " --incognito";//Get the link that the user types
                process.StartInfo.FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome";
                process.StartInfo.Arguments =a;
                process.Start();
                System.Threading.Thread.Sleep(2000);


                process.Kill();

It gives me an error that: Cannot process request because the process has exited.

and the break point is in the process.Kill(); line.

Looks like that chrome process is a launcher that opens another chrome process that contains the chrome browser and the launcher process is then closed. So you are closing a process that had already closed by itself.

This check helps you prevent killing the process before its terminated.

if( !process.WaitForExit(2000) ) {
   if (!process.HasExited) process.Kill();
}

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