Reputation: 35567
I have the following:
class Program {
static void Main(string[] args) {
Process pr;
pr = new Process();
pr.StartInfo = new ProcessStartInfo(@"notepad.exe");
pr.Disposed += new EventHandler(YouClosedNotePad);
pr.Start();
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
static void YouClosedNotePad(object sender, EventArgs e) {
Console.WriteLine("thanks for closing notepad");
}
}
When I close Notepad I don't get the message I was hoping to get - how do I amend so that closing notepad is returned to the console?
Upvotes: 3
Views: 221
Reputation: 236248
You need two things - enable raising events, and subscribing to Exited event:
static void Main(string[] args)
{
Process pr;
pr = new Process();
pr.StartInfo = new ProcessStartInfo(@"notepad.exe");
pr.EnableRaisingEvents = true; // first thing
pr.Exited += pr_Exited; // second thing
pr.Start();
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
Console.ReadKey();
}
static void pr_Exited(object sender, EventArgs e)
{
Console.WriteLine("exited");
}
Upvotes: 7
Reputation: 44931
You want to use the Exited event instead of Disposed:
pr.Exited += new EventHandler(YouClosedNotePad);
You also need to ensure that the EnableRaisingEvents property is set to true:
pr.EnableRaisingEvents = true;
Upvotes: 0