I think you are a little bit confused about the logic yourself. By the way here are some items you should use to do such a thing:
1- System.Diagnostics.Process.WaitForExit
With calling this method for a specific process instance, you can wait until the process is exited completely after you called the "Kill"
or "Shutdown"
method. It will close the main window in the process if it's a windows app. For the applications which have some confirmation boxes for exiting, it will wait until user confirm or cancel the dialog box (Like when your windows is shutting down while your applications are already open).
2- System.Diagnostics.Process.ExitCode
If you want to access the result of the exist message you send to a process, you should call the value of this property after calling "WaitForExit"
function. I guess you can use this property to find out if the first process exited or not and with what reason from the second process.
3- public static int Main
Because you need to access the exit code from the outside of the process you should change the return value of "Main"
method to int. This way you can return a value in the first process and access it in the second process through the "ExitCode"
property. If returning a value in the "Main"
function is difficult in your situation, you can pass the exit code by calling the 4th item.
4- System.Windows.Application.Shutdown (Exit in .NET < 4)
Calling this method does the exact thing as returning the value in the "Main"
function.
5- System.Diagnostics.Process.GetProcessesByName
After all by calling this static method you can get all the processes by specified name.
Hope it helps. I just tried to explain everything as simple as possible. These all are the tools you can use to implement the logic. The rest would be so simple if you know these abilities that .NET provided for you.
After all, as I've already forgot to say, if you are interested in doing the right thing, in my opinion and as my experience says, there is a technology that has been made just for these kind of logics.
6- .NET Interprocess Communications or IPC (a sample on code) (introduction on codeguru)
IPC is based on the .NET remoting (the most interesting discussion in .NET in my personal opinion) BUT regardless of what the name says, it's not only for remote communications. The remoting has many sections and on of those is the Inter-Process-Communication (IPC). It's the hard, but the best way if you ask me.
Cheers