System.Diaganostics.Process.Id是不一样的进程ID所示在任务管理器。为什么?所示、任务管理器、进程、Diaganostics

2023-09-03 07:42:27 作者:不浪漫的浪漫

我使用C#的 System.Diagnostic.Process 对象。 它的一个特性是编号。 该编号这将产生不一样的 PID 显示Windows任务管理器。 这是为什么?

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?

您看到的,一旦这个过程开始。 它启动其他两个未被管理的进程,对此我不能明确地得到标识由对象的属性引用。 我必须通过 System.Diagnostics.Process.GetProcesses通过所有程序搜索找到他们通过进程名()

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

我试图找到一种可靠的方法来终止该进程,并通过 PID ,一个显示在任务管理器。 有没有更好的办法?

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.

推荐答案

关键是,你不想要杀死你的进程标识。事实上,这是一个竞争条件:你生成的进程可能会死,而另一进程可能得到相同的ID创建。然后,当你去杀死它,你最终会杀死老,这就是一个已经死了的新工艺代替。

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.

下面是一些简单的code来实现作业类:

Here's some simple code to implement a Job class:

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 == IntPtr.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;
    }
}