c# - 为什么我的进程的 Exited 方法没有被调用?

标签 c# .net

我有以下代码,但为什么从未调用过 ProcessExited 方法?如果我不使用 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
}

最佳答案

为了在 Exited 事件上接收回调,EnableRaisingEvents必须设置为 true。

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

关于c# - 为什么我的进程的 Exited 方法没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4504170/

相关文章:

c# - 如何创建 IList 的实例?

c# - 代码审查 : CLR RegexSubstring

c# - 无法访问 asp :TextBox tag from the C# file : the name 'txtUsername' does not exist in the current context

c# - 使用 OpenID(通过 DotNetOpenAuth)以及用户角色和其他成员(member)提供者功能

c# - 从非异步代码调用异步方法

c# - 如何以编程方式调用键盘按键?

c# - 与 Java 相比,C# 中的命名约定

c# - 将字符串的值转换为 C# 中的类型

.net - 打印 "X"字符数与 "X"字符串长度的所有可能组合(暴力破解)

.net - 使用 "right".NET Framework 版本