Search code examples
c#.netpid

C# wait for external PID to exit


How do I wait until PID (not started from program itself) doesn't exist any longer? I want my program to get a PID from an external program via commandline parameter and just wait for it to exit and then do something.

I searched for it, but all I could find were examples where the process has been startet by the C# program itself. That's easy to manage ..

I was going with something like this, but it obviously doesn't work ..

Process[] pname = Process.GetProcessById(7860);
            if (pname.Length == 0)
                Console.WriteLine("nothing");
            else
                Console.WriteLine("run");

How can that be accomplished?


Solution

  • You can do that easily by writing:

    var p = Process.GetProcessById(7860);
    p.WaitForExit();