WScript.Echo() 当缓冲区大小增加 4MB 并向 Process.HasExited 返回错误值时挂起进程?

WScript.Echo() hangs the process when buffer size increses 4MB and returning false value to Process.HasExited?

我们添加了进程对象来执行脚本。脚本文件的主要方法在下一节中定义。当脚本执行时,WaitForExit() 方法返回 false。

System.Diagnostics.Process pProc = new System.Diagnostics.Process();
try  
  {
        pProc.StartInfo.FileName = scriptPath;
        pProc.StartInfo.Arguments = scriptArguments;
        pProc.StartInfo.UseShellExecute = false;
        pProc.StartInfo.RedirectStandardOutput = true;
        pProc.StartInfo.RedirectStandardError = true;
        pProc.StartInfo.WorkingDirectory = Path.GetDirectoryName(scriptPath);        
        pProc.Start();
        DateTime startTime = pProc.StartTime;
    
        bool bHasExited = pProc.WaitForExit(120 * 1000);               
    
        int timeElapsed = (int)(DateTime.Now - startTime).TotalSeconds;
        if (timeElapsed >= timeoutInSeconds && !bHasExited)
         {
             if (sError == null)
                 sError = "timeout and not Exited Properly"
             if (!pProc.HasExited)
                 pProc.Kill();
             bOk = false;
         }
         _stdOut = pProc.StandardOutput.ReadToEnd();
         _stdErr = pProc.StandardError.ReadToEnd();
          if (pProcess.ExitCode != 0)
           {
               if (sError == null)
                  sError = pProc.ExitCode.ToString();
               bOk = false;
            }
   }
   catch (Exception e)
   {   
       if (sError == null)
         sError = e.message;
       bOk = false;
    }

我们正在使用 cscript 来执行脚本文件。

function main
{
    var i;
    var exit_Code = 0;
    var str = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" ;
    
    for (i = 1; i <= 51; i++) {
        WScript.Echo(str);
    }
    WScript.Quit(0);
}

来自msdn

The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.