Process.Start 崩溃,没有异常
本文关键字:异常 Start 崩溃 Process | 更新日期: 2023-09-27 17:55:51
我正在开发一个简单的编译器,生成 IL 代码后的最后阶段是使用 ilasm
实用程序对其进行编译,这是崩溃发生的地方。
以下是该方法的完整代码(针对 Stack 稍作修改):
public static string ExecuteIL(string filename)
{
var ilasmp = new System.Diagnostics.Process ();
ilasmp.StartInfo.FileName = "ilasm";
ilasmp.StartInfo.Arguments = filename;
//Crash does not happen here:
ilasmp.Start ();
ilasmp.WaitForExit ();
var p = new System.Diagnostics.Process ();
p.StartInfo.FileName = "/usr/bin/time";
p.StartInfo.Arguments = "mono " + filename.Replace(".il", ".exe");
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
try{
//Crash happens HERE, but for some reason the exception does not get thrown
p.Start ();
}
catch{
throw new Exception ();
}
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit ();
return output;
}
只是为了说清楚:当我第一次打电话给Process.Start
时不会发生崩溃(ilasmp.Start ();
),但由于某种原因,这发生在以后(p.Start ();
),有趣的是,不会抛出异常。或者换句话说,代码只是崩溃。
如果要设置
UseShellExecute = true;
从Microsoft:
如果要将 RedirectStandardError 设置为 true,则必须将 UseShellExecute 设置为 false。否则,从标准错误流读取将引发异常。
使用外壳执行属性
重定向标准错误属性