views:

259

answers:

4

I'm using c#'s System.Diagnostic.Process object. One of its properties is Id. The Id this produces is not the same as the PID, shown in Windows Task Manager. Why is this?

You see, once this process is started. It launches two other unmanaged processes, for which I can't explicitly get IDs for by object property references. I have to search through all processes to find them by process name via System.Diagnostics.Process.GetProcesses().

I'm trying to find a reliable way to kill this process and all associated processes by PID, the one that shows in Task Manager. Is there a better way? I can't just kill all processes with the associated process names, because that might kill other instances of those processes that have nothing to do with my program.

+1  A: 

I can't reproduce this. I've just run the following code:

foreach (var proc in Process.GetProcesses()
                            .OrderBy(proc => proc.Id))
{
    Console.WriteLine("{0}: {1}", p.Id, p.ProcessName);
}

All the processes listed have matches the PID in Task Manager. This is using Windows 7 - what OS are you using? If you look at other processes, do they match the PID shown in Task Manager?

Jon Skeet
Jon, the other two processes (the unmanaged ones started indirectly by the process I start) match. But, the process I start's id is different in task manager, than it is in the property reference via myProcess.Id.
LonnieBest
BTW, I like that .OrderBy method you posted. I didn't know you could do that.
LonnieBest
+8  A: 

The key is that you don't want to kill your process by Id. In fact, that's a race condition: your spawned process could die and another process could get created with the same Id. Then when you go to kill it, you would end up killing the new process instead of the old one that was already dead.

The most reliable way to kill spawned processes is to put them in a Job object and terminate the Job once your task is completed.

Here's some simple code to handle the Job:

class Job
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string lpName);

    [DllImport("kernel32.dll")]
    public static extern bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess);

    [DllImport("kernel32.dll")]
    public static extern bool TerminateJobObject(IntPtr hJob, uint uExitCode);

    IntPtr job;

    public Process StartProc(string commandLine)
    {
        if (job == IntPtz.Zero)
            job = CreateJobObject(IntPtr.Zero, null);
        ProcessStartInfo si = new ProcessStartInfo(@"c:\windows\system32\cmd.exe");
        si.Arguments = "/c " + commandLine;
        si.CreateNoWindow = false;
        si.UseShellExecute = false;
        Process proc = Process.Start(si);
        AssignProcessToJobObject(job, proc.Handle);
        return proc;
    }

    public void TerminateProc()
    {
        // terminate the Job object, which kills all processes within it
        if (job != null)
            TerminateJobObject(job, 0);
        job = IntPtr.Zero;
    }
}
Gabe
I did imagine that possibility, but didn't know what to do. Will those unmanage-sub-processes (that are indirectly started by the process I start) be subordinately related to this job object too? So that if I kill the job, all 3 process get killed?
LonnieBest
BTW, I'm scared of code in angle brackets; I don't understand it yet. Are those attributes at the top? Is this code limited to 32 bit architecture?
LonnieBest
Terminating the Job object will terminate the process you started and every process started by it, and so on. And yes, those are attributes at the top in brackets. I haven't run this on a 64-bit machine, but there's no reason to believe it won't work.
Gabe
+1  A: 

* This is not the answer..but a question..thought this would be right place to continue this question * hi LonnieBest, i also faced this process id mis-match problem recently with windows 7...can u let me know if u have worked out on a way through which i can kill a process using process.id in vb.net ...also let me know (incase u have found out) ..why is the id different in the task manager than the one i see in process.id in windows 7!

Thanks a lot in advance :)

Read what Gabe said. Instead of killing the process, kill the job.
LonnieBest
A: 

@lonniebest..thanks for your reply.. I still have some doubt... ..how will we terminate individual processes using the above code... For eg.: I have opened google.com and yahoo.com through my windows appln...and if i wanna close only google.com... ..I know its possible but cant find a way of doing this in windows 7...

Please help..