我正在构建一个部署在 CentOS 7.2 上的 ASP.Net Core (netcore 1.1) 应用程序。
我有一个通过 System.Diagnostics.Process 调用外部进程(也是使用 .net core 构建的控制台应用程序)的操作,并且在返回之前不等待它退出。
问题是上述过程变成并保持不变<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>
除了终止其父进程(即我的 Core MVC 应用程序)之外,我没有其他方法可以摆脱它。
有没有办法在 .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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)