c# - Process.Start 崩溃无一异常(exception)

标签 c# linux mono process.start

我正在开发一个简单的编译器,生成 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.

UseShellExecute Property

RedirectStandardError Property

关于c# - Process.Start 崩溃无一异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38264751/

相关文章:

C#减去时间(小时分钟)

linux - Linux ptrace 怎么会不安全或包含竞争条件?

linux - Linux 何时调用 PCI 驱动程序的探测函数?

python - 包上传到 pypi.org 损坏

c# - Mono 3.4.1 无法在树莓派上正确编译

c# - 如何将从数据库检索的 smalldatetime 转换回 smalldatetime 以重新发布到数据库

c# - 为什么 Stream 类要实现 IDisposable?

c# - 连接到具有特定实例名称的 sql server 时 Mono 崩溃

mono - Mono 3.4.1中的UseCookieAuthentication

c# - Azure 上无法识别 WebAPI 路由