try-catch 中的 Visual Studio 2010 断点会导致捕获

本文关键字:断点 中的 Visual Studio 2010 try-catch | 更新日期: 2023-09-27 18:36:58

我在using行的尝试捕获中放置了一个断点。 另一个在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();
}

try-catch 中的 Visual Studio 2010 断点会导致捕获

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