ProcessStartInfo hanging on “WaitForExit”? Why?

前端 未结 22 2266
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:59

I have the following code:

info = new System.Diagnostics.ProcessStartInfo(\"TheProgram.exe\", String.Join(\" \", args));
info.CreateNoWindow = true;
info.Win         


        
22条回答
  •  清歌不尽
    2020-11-22 03:05

    Credit to EM0 for https://stackoverflow.com/a/17600012/4151626

    The other solutions (including EM0's) still deadlocked for my application, due to internal timeouts and the use of both StandardOutput and StandardError by the spawned application. Here is what worked for me:

    Process p = new Process()
    {
      StartInfo = new ProcessStartInfo()
      {
        FileName = exe,
        Arguments = args,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true
      }
    };
    p.Start();
    
    string cv_error = null;
    Thread et = new Thread(() => { cv_error = p.StandardError.ReadToEnd(); });
    et.Start();
    
    string cv_out = null;
    Thread ot = new Thread(() => { cv_out = p.StandardOutput.ReadToEnd(); });
    ot.Start();
    
    p.WaitForExit();
    ot.Join();
    et.Join();
    

    Edit: added initialization of StartInfo to code sample

提交回复
热议问题