尝试使用C#进程在管理模式下加载第三方应用程序并读取其输出。然而,当我尝试在进程上使用RedirectStandardOutput时,在Windows7电脑上启动几秒钟后,它就崩溃了。我可以让它在Windows10上运行得很好,但我们也需要它在Windows7上运行。我希望有一种方法,让这个进程能够正常运行,并将它的输出读取到程序中,而不会在C#试图加载它时崩溃。
我已经将进程隔离为只有在重定向标准输出或标准错误时才会崩溃。将UseShellExecute设置为false似乎不会影响它。当我尝试像这样加载命令提示符时,命令提示符同样会关闭,所以这似乎不是程序的怪癖。
我也尝试过通过使用FileSystemWatcher和读取程序可以设置为输出到这些文件的日志来绕过它,但当我尝试时,输出似乎没有以足够好的速度刷新到文件中。
在startInfo中添加"runas“动词似乎也不会影响它,因为这个过程已经要求应用程序在管理员模式下启动。
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = filename;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
if(case1){
p.StartInfo.Arguments = args;
p.Start();
}在我最初的Windows10开发环境中发生的预期结果是,该过程将启动
在Windows7上的实际结果是,程序在启动时冻结,窗口弹出“此应用程序没有响应”消息。
EventViewer似乎认为问题出在msvcr120.dll上,但事实是,在3台独立的Windows7电脑上发生了这种情况,这表明可能是其他原因。
发布于 2019-06-08 11:53:04
这可能是因为您正在重定向输出,而不是读取它。子进程将在填满其输出缓冲区时挂起。确保您是在C#代码中读取它。类似于:
using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += (sender, args) => Debug.WriteLine("STDOUT: " + args.Data);
p.ErrorDataReceived += (sender, args) => Debug.WriteLine("STDERR:" + args.Data);
p.StartInfo.FileName = exePath;
p.StartInfo.Arguments = parameters;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit(30 * 1000);
if (!p.HasExited)
{
Debug.WriteLine("Killing process");
p.Kill();
}
}至少,子可执行文件可能正在向其stderr写入错误,在这种情况下,问题将出现在您的Debug输出中。
https://stackoverflow.com/questions/56500784
复制相似问题