ProcessStartInfo hanging on “WaitForExit”? Why?

前端 未结 22 1853
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:19

    Workaround I ended up using to avoid all the complexity:

    var outputFile = Path.GetTempFileName();
    info = new System.Diagnostics.ProcessStartInfo("TheProgram.exe", String.Join(" ", args) + " > " + outputFile + " 2>&1");
    info.CreateNoWindow = true;
    info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    info.UseShellExecute = false;
    System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
    p.WaitForExit();
    Console.WriteLine(File.ReadAllText(outputFile)); //need the StandardOutput contents
    

    So I create a temp file, redirect both the output and error to it by using > outputfile > 2>&1 and then just read the file after the process has finished.

    The other solutions are fine for scenarios where you want to do other stuff with the output, but for simple stuff this avoids a lot of complexity.

提交回复
热议问题