Reputation: 16724
I'm using this:
var proc2 = Process.Start(Path.GetFullPath(filename));
proc2.Exited += (_, __) =>
{
MessageBox.Show("closed!");
};
But I close the window and don't get MessageBox.Show("closed!");
. How to fix this?
Upvotes: 1
Views: 1571
Reputation: 1162
You forgot to set the EnableRaisingEvents
to true.
Also, you may want to create a Process with the constructor, set the ProcessStartInfo and then call Start after you register to listen to the event. Otherwise you have a race condition where the Process exits before you even register to listen for the event (unlikely I know, but not mathematically impossible).
var process = new Process();
process.StartInfo = new ProcessStartInfo(Path.GetFullPath(filename));
process.EnableRaisingEvents = true;
process.Exited += (a, b) =>
{
MessageBox.Show("closed!");
};
process.Start();
Upvotes: 6
Reputation: 3443
you forget Enable Events
Process p;
p = Process.Start("cmd.exe");
p.EnableRaisingEvents = true;
p.Exited += (sender, ea) =>
{
System.Windows.Forms.MessageBox.Show("Cmd was Exited");
};
Upvotes: 4