简体   繁体   English

为什么我的进程的退出方法没有被调用?

[英]Why is my process's Exited method not being called?

I have following code, but why is the ProcessExited method never called? 我有以下代码,但为什么从未调用ProcessExited方法? It is the same if I don't a use Windows shell ( startInfo.UseShellExecute = false ). 如果我不使用Windows shell( startInfo.UseShellExecute = false ),也是一样的。

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);

try
{
     Process correctionProcess = Process.Start(startInfo);
     correctionProcess.Exited += new EventHandler(ProcessExited);                   

     correctionProcess.WaitForExit();

     status = true;
}

..... .....

internal void ProcessExited(object sender, System.EventArgs e)
{
      //print out here
}

In order to receive a callback on Exited event, the EnableRaisingEvents must be set to true. 要在Exited事件上接收回调,必须将EnableRaisingEvents设置为true。

Process correctionProcess = Process.Start(startInfo);
correctionProcess.EnableRaisingEvents = true;
correctionProcess.Exited += new EventHandler(ProcessExited); 

From MSDN : 来自MSDN

The Exited event indicates that the associated process exited. 退出事件表示相关进程已退出。 This occurrence means either that the process terminated (aborted) or successfully closed. 这种情况意味着流程终止(中止)或成功关闭。 This event can occur only if the value of the EnableRaisingEvents property is true. 仅当EnableRaisingEvents属性的值为true时,才会发生此事件。

Have you set that property to true? 您是否将该属性设置为true?

您必须将Process.EnableRaisingEvents设置为true

设置correctionProcess.EnableRaisingEvents = true

I've come across examples that place new Process() in a using clause. 我遇到过将new Process()放在using子句中的示例。 Do not do that if you want to use the Exited feature. 如果要使用“已Exited功能,请不要这样做。 The using clause destroys the instance along with any event handles on Exited . using子句会将实例与Exited上的任何事件句柄一起销毁。

This... 这个...

using(var process = new Process())
{
   // your logic here
}

Should be this... 应该是......

var process = new Process();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM