Reputation: 21
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.
Upvotes: -1
Views: 38
Reputation: 21
Apparently, I did not google enough. This is quite easy o achieve with WMI queries. This code shows how one can isolate all the instances of the process javaw.exe and check any WMI parameter (e.g. the CommandLine)
public static void Main()
{
SelectQuery query = new SelectQuery("Select * from Win32_Process WHERE Name = 'javaw.exe'" );
ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
try
{
foreach (ManagementObject processes in searcher.Get())
{
Console.WriteLine(processes["CommandLine"].ToString() + Environment.NewLine);
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e);
}
Console.Read();
}
Here a guide that shows how to create an event watcher base don WMI query https://www.codeproject.com/Articles/12138/Process-Information-and-Notifications-using-WMI It is an old post, and you will need to update part of the code.
Upvotes: 0