I have parent process that opens child process . I need to perform some functionality only when the parent process is no more running.
What is the best way to know that the parent process is not running ? Because it can be terminated violently then I don’t want to make some functionality that will send signal to my child process on the closing event.
Or just looking for my parent process like that:
In the parent make this and pass it to the child Process.GetCurrentProcess().Id
And in the child every several milliseconds check this one
Process localById = Process.GetProcessById(1234);
Any ideas ? Recommendations ..
Here is a simple example how to use
Process.WaitForExitto check for a parent process whose id has been passed on the command line:Using the
Process.Exitedevent could be done like this:Note that in both samples the purpose of the
AutoResetEventis solely to prevent your main thread from exiting. In a Windows Forms application you would not need to use it as your program will be in a message loop and only exit if you close it.