The issue is how to track a process I start from my application.
I am doing a "launcher" and I would like to track when the program I launch is closed.
I checked these links, but I think it is not exactly the same I am trying because the processes I will launch are all of them javaw.exe
and I need to treat them individually.
How to detect a process start & end using c# in windows?
This is how the code looks now:
{
Process proc = new Process();
proc.Exited += EclipseInstanceClosed;
proc.Disposed += EclipseInstanceDisposed;
// this property is a *.exe path: 'C:\eclipse\eclipseV4.4.1\eclipse.exe' for example
proc.StartInfo.FileName = SelectedEclipseInstance.Exec;
proc.StartInfo.UseShellExecute = true;
proc.Start();
}
...
void EclipseInstanceClosed(object sender, EventArgs e)
{
MessageBox.Show("Eclipse was closed!");
}
void EclipseInstanceDisposed(object sender, EventArgs e)
{
MessageBox.Show("Eclipse was disposed!");
}
But these events are not raised when I close the program.
Any idea about it?
Update:
I also tried this solution(why Process's Exited method not being called? ) with proc.EnableRaisingEvents = true;
but with no success
Update II:
I modified the code so the process is started within a Task. In this case the events are raised. What is the reason?
LaunchEclipseProcess();
...
private async void LaunchEclipseProcess()
{
await Task.Run(() =>
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = true;
proc.Exited += EclipseInstanceClosed;
proc.Disposed += EclipseInstanceDisposed;
proc.StartInfo.FileName = SelectedEclipseInstance.Exec;
proc.Start();
});
}