ProcessStartInfo hanging on “WaitForExit”? Why?

前端 未结 22 1850
佛祖请我去吃肉
佛祖请我去吃肉 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:01

    I solved it this way:

                Process proc = new Process();
                proc.StartInfo.FileName = batchFile;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;      
                proc.Start();
                StreamWriter streamWriter = proc.StandardInput;
                StreamReader outputReader = proc.StandardOutput;
                StreamReader errorReader = proc.StandardError;
                while (!outputReader.EndOfStream)
                {
                    string text = outputReader.ReadLine();                    
                    streamWriter.WriteLine(text);
                }
    
                while (!errorReader.EndOfStream)
                {                   
                    string text = errorReader.ReadLine();
                    streamWriter.WriteLine(text);
                }
    
                streamWriter.Close();
                proc.WaitForExit();
    

    I redirected both input, output and error and handled reading from output and error streams. This solution works for SDK 7- 8.1, both for Windows 7 and Windows 8

提交回复
热议问题