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

好久不见. 提交于 2019-12-01 11:38:58

问题


I'm using this:

var proc2 = Process.Start(Path.GetFullPath(filename));
proc2.Exited += (_, __) =>
{
    MessageBox.Show("closed!");
};

But I close the window and don't get MessageBox.Show("closed!");. How to fix this?


回答1:


You need to set Process.EnableRaisingEvents to true.




回答2:


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();



回答3:


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



回答4:


You can fire the alert after proc2.WaitForExit()



来源:https://stackoverflow.com/questions/10920964/how-to-know-when-a-process-created-by-process-start-was-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!