I am trying to create an external launcher for an app, which I recently noticed is running in Java.. I did not immediately noticed because the program has an .exe extension and I assumed it was an executable directly running the software. Instead, it appears that this is an initial container than then open Java and run the software in Java.
The launcher I wrote works for apps like Notepad, but the structure I have does not work with the situation I have found myself in.
Here the (simplified) code I am using.
Button on a form launch the exe using this
MyProcess = Process.Start(processPath);
MyProcess.EnableRaisingEvents = true;
MyProcess is a process class with a private class associated to it. I got the code from another answer here.
private Process withEventsField_MyProcess;
Process MyProcess
{
get { return withEventsField_MyProcess; }
set
{
if (withEventsField_MyProcess != null)
{
withEventsField_MyProcess.Exited -= MyProcess_Exited;
}
withEventsField_MyProcess = value;
if (withEventsField_MyProcess != null)
{
withEventsField_MyProcess.Exited += MyProcess_Exited;
}
}
}
Whenever the event is triggered, I run my event code
private void MyProcess_Exited(object sender, System.EventArgs e)
{
//do stuff here
Application.Exit();
}
The problem with this is that I was planning to monitor an executable, and instead I find myself with an exe that stats java and then exits, and my events detects that as the exit event for the whole system.
I am trying to find a simple solution that allows me to detect when the specific Java app is closed so I can execute the rest of the program.
While I am writing this as a generic question for future users, I have the hint of an idea, but no clear view about how to implement it (assuming the idea is good at all).
I found out that via cmd I can get processes with also the command line executed to call them. In the case of the Java program I want to monitor, the command line would work as a unique identifier. Therefore, I should be able to hook up into that to determine which process I want to monitor for the exited event. And this is where I hit the wall.
Any help would be much appreciated.