How to know when a process created by Process.Start() was closed?

前端 未结 4 1960
旧时难觅i
旧时难觅i 2021-01-16 06:08

I\'m using this:

var proc2 = Process.Start(Path.GetFullPath(filename));
proc2.Exited += (_, __) =>
{
    MessageBox.Show(\"closed!\");
};
<
相关标签:
4条回答
  • 2021-01-16 06:17

    You can fire the alert after proc2.WaitForExit()

    0 讨论(0)
  • 2021-01-16 06:20

    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();
    
    0 讨论(0)
  • 2021-01-16 06:33

    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");
                };
    
    0 讨论(0)
  • 2021-01-16 06:40

    You need to set Process.EnableRaisingEvents to true.

    0 讨论(0)
提交回复
热议问题