Fix the EventHandler to tell me when the notebook is closed
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 change to close Notepad to the console?
+3
source to share
2 answers
You need two things, enable event assembly and subscribe to Exited :
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");
}
+7
source to share
You want to use Exited instead of Disposed:
pr.Exited += new EventHandler(YouClosedNotePad);
You also need to make sure the EnableRaisingEvents property is set to true:
pr.EnableRaisingEvents = true;
0
source to share