2013-10-25 41 views
1

我在using線上試穿了一個斷點。和另一個在result.wasSuccessful線。try-catch中的Visual Studio 2010斷點導致catch?

當我這樣做,它會去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(); 
} 
+3

看起來像一個定時問題 – VladL

+3

您正在啓動的另一過程。 Visual Studio調試器能夠凍結當前進程的執行並逐行執行(跨多個線程),但它與您未啓動的進程沒有相同的作用。也許你不應該調用靜態方法Process.Start並簡單地創建一個新的Process(),然後設置它的屬性,最後調用process.Start()或者process.WaitForExit()方法)? –

回答

0

通過你擊中位於使用斷點時,進程已經啓動,執行和退出,使工藝對象無效,並導致錯誤。事實上,即使沒有中斷點,您也有可能導致問題發生的競賽狀況。 您應該在設置所有必要屬性後推遲啓動過程。另外,你應該放棄使用聲明。 以下示例提供瞭如何讀進程的標準輸出和標準錯誤流的極好例子:ProcessStartInfo.RedirectStandardOutput Property

相關問題