我试图从.NET程序调用php-cgi.exe.我使用RedirectStandardOutput将输出作为流返回,但整个过程非常慢.
你对我如何能加快速度有任何想法吗?还有其他技术吗?
Dim oCGI As ProcessStartInfo = New ProcessStartInfo() oCGI.WorkingDirectory = "C:\Program Files\Application\php" oCGI.FileName = "php-cgi.exe" oCGI.RedirectStandardOutput = True oCGI.RedirectStandardInput = True oCGI.UseShellExecute = False oCGI.CreateNoWindow = True Dim oProcess As Process = New Process() oProcess.StartInfo = oCGI oProcess.Start() oProcess.StandardOutput.ReadToEnd()
Jader Dias.. 17
我发现的最佳解决方案是:
private void Redirect(StreamReader input, TextBox output) { new Thread(a => { var buffer = new char[1]; while (input.Read(buffer, 0, 1) > 0) { output.Dispatcher.Invoke(new Action(delegate { output.Text += new string(buffer); })); }; }).Start(); } private void Window_Loaded(object sender, RoutedEventArgs e) { process = new Process { StartInfo = new ProcessStartInfo { CreateNoWindow = true, FileName = "php-cgi.exe", RedirectStandardOutput = true, UseShellExecute = false, WorkingDirectory = @"C:\Program Files\Application\php", } }; if (process.Start()) { Redirect(process.StandardOutput, textBox1); } }
这实际上是我见过的重定向输出的最佳解决方案......谢谢Jader! (2认同)
Bob King.. 8
您可以使用OutputDataReceived事件接收数据,因为它被泵送到StdOut.
我发现的最佳解决方案是:
private void Redirect(StreamReader input, TextBox output) { new Thread(a => { var buffer = new char[1]; while (input.Read(buffer, 0, 1) > 0) { output.Dispatcher.Invoke(new Action(delegate { output.Text += new string(buffer); })); }; }).Start(); } private void Window_Loaded(object sender, RoutedEventArgs e) { process = new Process { StartInfo = new ProcessStartInfo { CreateNoWindow = true, FileName = "php-cgi.exe", RedirectStandardOutput = true, UseShellExecute = false, WorkingDirectory = @"C:\Program Files\Application\php", } }; if (process.Start()) { Redirect(process.StandardOutput, textBox1); } }
您可以使用OutputDataReceived事件接收数据,因为它被泵送到StdOut.