【问题标题】:Visual Studio 2010 breakpoints inside try-catch causes a catch?try-catch 中的 Visual Studio 2010 断点会导致捕获吗?
【发布时间】:2013-10-27 13:27:37
【问题描述】:

我在 using 行的 try-catch 中设置了一个断点。另一个在result.wasSuccessful 行。

当我这样做时,它会转到 catch 并说它返回一个错误。

但是,当using 行没有断点时,不会捕获任何错误。

为什么会这样?

这是错误

+       $exception  {"Cannot process request because the process (12400) has exited."}  System.Exception {System.InvalidOperationException}

try
{
   using (Process exeProcess = Process.Start(psi))
   {
      exeProcess.PriorityClass = ProcessPriorityClass.High;
      var outString = new StringBuilder(100);
      exeProcess.OutputDataReceived += (s, e) => outString.AppendLine(e.Data);
      exeProcess.BeginOutputReadLine();

      var errString = exeProcess.StandardError.ReadToEnd();

      if (!string.IsNullOrEmpty(errString))
      {
         result.WasSuccessful = false;
         result.ErrorMessage = errString;
      }
   }
}
catch (Exception ex)
{
   result.WasSuccessful = false;
   result.ErrorMessage = ex.ToString();
}

【问题讨论】:

  • 看起来是时间问题
  • 您正在启动另一个进程。 Visual Studio 调试器能够冻结当前进程的执行并逐行(跨多个线程)逐步执行,但它与您正在启动的进程不同,因为它没有附加。也许你不应该调用静态方法Process.Start,而只是创建一个new Process(),然后设置它的属性,然后最后调用process.Start(),也许是process.WaitForExit()(实例方法)?

标签: c# visual-studio-2010


【解决方案1】:

当你到达 using 处的断点时,进程已经启动、执行和退出,导致 Process 对象无效并导致错误。事实上,你有一个竞争条件,即使没有断点也可能导致问题发生。 您应该在设置所有必要的属性后推迟启动该过程。此外,您应该取消 using 语句。 下面的例子提供了一个很好的例子来说明如何读取进程的标准输出和标准错误流:ProcessStartInfo.RedirectStandardOutput Property

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多