我正在构建一个部署在 CentOS 7.2 上的 ASP.Net Core (netcore 1.1) 应用程序。
我有一个通过 System.Diagnostics.Process 调用外部进程(也是用 .net 核心构建的控制台应用程序)的操作,并且在返回之前不等待它退出。
问题是该进程变成并停留在 <defunct>
即使在它完成执行之后。我不想等待它退出,因为该过程可能需要几分钟才能完成其工作。
这是一个示例代码
//The process is writing its progress to a sqlite database using a
//previously generated guid which is used later in order to check
//the task's progress
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/bin/sh -c \"/path/to/process/executable -args\"";
psi.UseShellExecute = true;
psi.WorkingDirectory = "/path/to/process/";
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;
psi.RedirectStandardInput = false;
using(Process proc = new Process({ StartInfo = psi }))
{
proc.Start();
}
该过程开始并完成其工作。它将其特定任务的进度写入 sqlite 数据库。然后我可以探测该数据库以检查进度。
一切运行正常,但我可以在进程执行后看到
ps -ef |grep executable
它被列为 <defunct>
我没有其他方法可以摆脱它,只能杀死它的父进程,即我的 CoreMVC 应用程序。有没有办法在 .NET Core 应用程序中启动进程而不等待它退出,并强制父应用程序获取结果
<defunct>
子进程?
最佳答案
我通过允许进程引发事件以某种方式修复了它:
using(Process proc = new Process(
{
StartInfo = psi,
EnableRaisingEvents = true //Allow the process to raise events,
//which I guess triggers the reaping of
//the child process by the parent
//application
}))
{
proc.Start();
}
关于asp.net-core-mvc - .NET Core : Process. Start() 留下 <defunct> 子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515360/