我正在开发一个简单的编译器,生成 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;
来自微软:
You must set UseShellExecute to false if you want to set RedirectStandardError to true. Otherwise, reading from the StandardError stream throws an exception.
关于c# - Process.Start 崩溃无一异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38264751/