abhi
abhi

Reputation: 3136

Kill method of process not working

I am trying to test the following code snippet.

        static void StartAndKill()
    {
        Process ieProc = Process.Start("iexplore.exe", "www.apress.com");
        Console.WriteLine("--> Hit enter to kill {0}\t", ieProc.ProcessName);
        Console.ReadLine();
        try
        {
            Console.WriteLine(ieProc.Id);
            ieProc.Kill();

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

This should kill the internet explorer window and close it. Instead I get an exception that says:

Cannot process request because the process (7256) has exited.

What is the logical explanation for this behaviour?

Upvotes: 1

Views: 1572

Answers (2)

abhi
abhi

Reputation: 3136

The error message is misleading. The real reason why the process doesn't get killed is administrative privs on Windows 7 are not defaulted. If I open a command prompt by using "Run as administrator", the code does what it is supposed to do. I will move to close this question.

Thanks to all those who responded.

Upvotes: 1

Polyfun
Polyfun

Reputation: 9639

If the iexplore process is already running (check Task Manager), then Process.Start may use the existing process - but the process id you get back is not for this existing process, but a dummy process that starts and then exits. You could enumerate the existing iexplore processes and check their titles to find the correct one, and then get its process id and kill it.

Upvotes: 2

Related Questions