簡體   English   中英

Visual Studio調試-System.Diagnostics.Process掛在WaitForExit上

[英]Visual Studio debug - System.Diagnostics.Process hangs on WaitForExit

在Windows 10,Visual Studio 2015,.NET 4.6控制台應用程序中,我在Main中調用了稱為TestProcess的單個方法。 構建模式調試,如果我在未調試的情況下運行應用,則會打印正確的文本:

827ccb0eea8a706c4c34a16891f84e7b * test.txt

按任意鍵繼續 。

如果我通過調試運行應用程序,則需要等待3秒鍾才能打印

錯誤錯誤錯誤錯誤''''

這只是對實際問題的簡化,該代碼是某些復雜代碼的基礎,這些代碼在無需調試md5sums.exe的情況下也掛在發行版中,但可用於某些其他程序。 復雜代碼也掛在var a = proc.WaitForExit(timeout);上。 直到超時,如所附示例所示。 另一方面,這種簡化將在沒有調試器的情況下發布。 同樣,所有這些問題都始於Windows 10,在Windows 7上一切正常。

[編輯]無法理解md5sums.exe為什么會引起問題,如果我使用其他東西(即)。 FileName =“ ping”,參數=“ localhost”,一切正常。

[EDIT2]我的復雜程序在Windows 10上停止工作(發布-未經調試運行),但是此示例也掛在Windows 7上(調試-通過調試運行)

        static void TestProcess()
        {
            using (var proc = new Process())
            {
                proc.StartInfo.FileName = "md5sums.exe";
                proc.StartInfo.WorkingDirectory = @"C:\Temp\ProcessDebug";
                proc.StartInfo.Arguments = "-u test.txt";
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                StringBuilder output = new StringBuilder();
                StringBuilder error = new StringBuilder();
                using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    proc.OutputDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                            outputWaitHandle.Set();
                        else
                            output.AppendLine(e.Data);
                    };
                    proc.ErrorDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                            errorWaitHandle.Set();
                        else
                            error.AppendLine(e.Data);
                    };
                    proc.Start();
                    proc.BeginOutputReadLine();
                    proc.BeginErrorReadLine();
                    var timeout = 1000;
                    var a = proc.WaitForExit(timeout);
                    var b = outputWaitHandle.WaitOne(timeout);
                    var c = errorWaitHandle.WaitOne(timeout);
                    if (a && b && c)
                        Console.WriteLine(output.ToString());
                    else
                        Console.WriteLine($"Error {a} {b} {c} '{output}' '{error}'");
                }
            }
        }

有3個要解決的問題:

  • 在某些情況下,使用我的設置啟動md5sums.exe后,它會暫停執行:

827ccb0eea8a706c4c34a16891f84e7b * test.txt

按ENTER退出

如果將CreateNoWindow設置為false並刪除了stderr重定向,則可以觀察到此錯誤。 這可以通過使用-e開關來解決: '立即退出; 在返回之前不要暫停' 這將解決所有情況。 但是由於我沒有使用-ei,所以取決於調試器和Windows版本的行為不一致。

  • 當運行時沒有調試時,盡管所有設置都相同,但沒有觸發暫停,並且沒有輸出“按ENTER退出”。 但是使用調試運行會導致暫停程序直到超時為止,直到程序超時為止,此時md5sums將掛在任務管理器中,等待Enter。
  • 在發布模式下,盡管在Windows 7上輸出了暫停並且已在輸出中輸出了“按Enter退出”,但仍未調試而運行md5sums返回,並且繼續執行而不會阻塞並達到超時。 在Windows 10上不是這種情況,在Windows 10中,md5sums將駐留在任務管理器中,等待Enter,並且在達到超時后程序繼續執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM