2

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?

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Leo.1906
  • 153
  • 2
  • 11
  • @RaymondChen: Thats not exactly the same as in the referenced question they are waiting for a process started in the very same code. –  Sep 22 '16 at 14:22
  • @wonko79: The part not addressed in the duplicate Q&A is the impossible part. The one where a random process magically finds the interested application's standard I/O handles, just at the right time, and uses them to pipe their process ID into. – IInspectable Sep 22 '16 at 20:30
  • [This answer](http://stackoverflow.com/a/6723758/902497) is the specific duplicate. "Create/Attach to the process and then either use `WaitForExit()` to block until it has exited, or use the `OnExited` event if you don't wish your application to block while it's waiting for the app to exit." This particular question is using the "attach" case. – Raymond Chen Sep 22 '16 at 22:55

1 Answers1

2

You can do that easily by writing:

var p = Process.GetProcessById(7860);
p.WaitForExit();
  • Are you the same @wonko79, that [just complained](http://stackoverflow.com/questions/39641494/c-sharp-wait-for-external-pid-to-exit/39641632#comment66586778_39641494) that the proposed duplicate wouldn't address the question? Only to post an answer that - by that same standard - doesn't address the question either. I don't get that line of reasoning. Unless you are another wonko79, of course. – IInspectable Sep 22 '16 at 20:33