C# Ngnix进程在等待退出时挂起
我正试图通过C代码安装并运行ngnix。当我手动或在命令提示符下调用它们时,它工作正常,但当我以编程方式启动一个进程时,它会挂起,等待进程退出。 下面是我如何调用它们的C# Ngnix进程在等待退出时挂起,c#,nginx,cmd,process,C#,Nginx,Cmd,Process,我正试图通过C代码安装并运行ngnix。当我手动或在命令提示符下调用它们时,它工作正常,但当我以编程方式启动一个进程时,它会挂起,等待进程退出。 下面是我如何调用它们的 var filePath = "c:\Install\Nginx\nginx.exe";\\also tried to execute the batch file instead of .exe, dint work var workingDIR = System.IO.Path.GetDirect
var filePath = "c:\Install\Nginx\nginx.exe";\\also tried to execute the batch file instead of .exe, dint work
var workingDIR = System.IO.Path.GetDirectoryName(filePath);
isSuccess = Execute(filePath, string.Empty, out resultCode, workingDIR);
这里是Execute(…)定义。注意,我使用了相同的Execute()方法来启动不同的.exe进程,它也可以正常工作。不确定,这是我调用ngnix的方式还是进程启动的方式。我还尝试执行批处理文件而不是.exe,但无法正常工作
谢谢你能帮我理解为什么它会挂在这里
bool Execute(string fileName, string args, out int resultcode, string workingDir)
{
var returnValue = true;
resultcode = -1;
try
{
if (File.Exists(fileName))
{
using (var process = new Process())
{
if (!fileName.StartsWith("\""))
{
fileName = $"\"{fileName}\"";
}
var startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = fileName;
startInfo.Arguments = args;
if (workingDir != null)
{
startInfo.WorkingDirectory = workingDir;
}
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
process.StartInfo = startInfo;
process.OutputDataReceived += WriteToOutputFile;
process.ErrorDataReceived += WriteToOutputFile;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
//<seems to hang here>
process.WaitForExit();
resultcode = process.ExitCode;
}
}
else
{
Log.Write($"Exe not found: [{fileName}].");
}
}
catch (Exception ex)
{
Log.Write(ex);
returnValue = false;
}
return returnValue;
}
bool-Execute(字符串文件名、字符串参数、out-int-resultcode、字符串工作目录)
{
var返回值=真;
结果代码=-1;
尝试
{
if(File.Exists(fileName))
{
使用(var进程=新进程())
{
如果(!fileName.StartsWith(“\”))
{
文件名=$“\”{fileName}\”;
}
var startInfo=new ProcessStartInfo();
startInfo.CreateNoWindow=true;
startInfo.UseShellExecute=false;
startInfo.FileName=文件名;
startInfo.Arguments=args;
if(workingDir!=null)
{
startInfo.WorkingDirectory=workingDir;
}
startInfo.RedirectStandardOutput=true;
startInfo.RedirectStandardError=true;
process.StartInfo=StartInfo;
process.OutputDataReceived+=WriteToOutputFile;
process.ErrorDataReceived+=WriteToOutputFile;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
//
process.WaitForExit();
resultcode=process.ExitCode;
}
}
其他的
{
Log.Write($“未找到Exe:[{fileName}]”);
}
}
捕获(例外情况除外)
{
日志写入(ex);
returnValue=false;
}
返回值;
}