asp.net-core-mvc - .NET Core : Process. Start() 留下 <defunct> 子进程

标签 asp.net-core-mvc .net-core

我正在构建一个部署在 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/

相关文章:

c# - 内联变量声明不编译

c# - ASP.Net Core MVC UseExceptionHandler 导致空白页面

c# - 从启动类访问配置对象

.net - 了解 .Net Core 和 Mono

c# - 在 Windows 上,ManifestEmbeddedFileProvider() 抛出异常

asp.net-core - 在哪里可以找到 ASP.NET Core 2 源代码?专用于 Microsoft.AspNetCore.Authentication.OpenIdConnect

c# - 如何允许所有 https 而不管 .NET Core HttpClient 中的有效性?

c# - 用户已通过身份验证,但访问 token 在哪里?

c# - 仅当不记名 header 存在时才验证 JWT

asp.net - 在 ASP.NET Core MVC 中嵌套 TagHelpers