2

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.

Dionisis
  • 55
  • 1
  • 6

2 Answers2

1

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.

Xela
  • 2,322
  • 1
  • 17
  • 32
  • so should i have to find the exact process for my case ? I mean that one that stands for the incognito chrome – Dionisis Mar 30 '15 at 21:58
  • 1
    You might be able to use WMI to find the children of that process and then exit them, or hook the CreateProcessA and CreateProcessW WINAPI methods using hot patching and send the child ids back using named pipes(Might be an overkill) or try and find the process via its window name like you said for incognito window. Someone else might have an idea I haven't thought of. – Xela Mar 30 '15 at 22:06
-1

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

if( !process.WaitForExit(2000) ) {
   if (!process.HasExited) process.Kill();
}
HappyLee
  • 435
  • 4
  • 11
  • He was advised above that the chrome process he is trying to kill is ALREADY being killed as a result of it being a launcher to another chrome process. In his case, he needs to find the new incognito process which is being launched and kill that. Your answer does not sound useful in this situation. – Magic Mick Mar 31 '15 at 00:16