3

我有以下内容:

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");
    }

}

当我关闭记事本时,我没有收到我希望收到的消息 - 如何修改以使关闭记事本返回到控制台?

4

2 回答 2

7

您需要两件事 -启用引发事件和订阅退出事件:

    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");
    }
于 2013-01-19T20:33:40.130 回答
0

您想使用Exited事件而不是 Disposed:

pr.Exited += new EventHandler(YouClosedNotePad);

您还需要确保EnableRaisingEvents属性设置为 true:

pr.EnableRaisingEvents = true;
于 2013-01-19T20:32:16.367 回答