我有以下内容:
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");
}
}
当我关闭记事本时,我没有得到我希望得到的消息 - 如何修改以便关闭记事本返回到控制台?
答案 0 :(得分:7)
您需要两件事 - enable raising events,并订阅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");
}
答案 1 :(得分:0)
您想使用Exited事件而不是Disposed:
pr.Exited += new EventHandler(YouClosedNotePad);
您还需要确保EnableRaisingEvents属性设置为true:
pr.EnableRaisingEvents = true;