c# - 处理子进程的退出事件

标签 c# .net process console-application

我有一个控制台应用程序和 Main 方法。我启动了一个类似下面代码的进程,当进程存在时,进程的 Exist 事件被触发,但它也关闭了我的控制台应用程序,我只想启动一个进程,然后在该进程的退出事件中启动另一个进程。

进程输出也反射(reflect)在我的主控制台应用程序中。


Process newCrawler = new Process();
newCrawler.StartInfo = new ProcessStartInfo();
newCrawler.StartInfo.FileName = configSection.CrawlerPath;
newCrawler.EnableRaisingEvents = true;
newCrawler.Exited += new EventHandler(newCrawler_Exited);

newCrawler.StartInfo.Arguments = "someArg";
newCrawler.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
newCrawler.StartInfo.UseShellExecute = false;
newCrawler.Start();

最佳答案

您必须调用 newCrawler.WaitForExit() 才能一直保持到子进程完成。然后你可以使用 newCrawler.ExitCode 来获得退出值。

关于c# - 处理子进程的退出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4689673/

相关文章:

c# - 如何使用 nHibernate 中的子集之一来过滤项目

c# - C# 中的 0.0/0.0 不会抛出 "Attempted to divide by zero."?

c# - 使用黑白图像进行 Blob 跟踪

并发编程——是否需要手动锁定多个进程将访问的文件?

c# - 在 WPF XAML 中,如何连接 2 个常量以便使用预定义路径?

.net - Silverlight 3 中没有的 WPF 功能列表

c# - .Net Framework 4 语音识别——使用小语法时的误报

c# - SmtpClient - 什么是正确的生命周期?

c++ - 子进程为 cmd 时 GenerateConsoleCtrlEvent 崩溃

c++ - 从 Linux 中的非子进程获取退出代码