在.NET中有效地重定向标准输出

时间:2022-05-22 15:49:04

I am trying to call php-cgi.exe from a .NET program. I use RedirectStandardOutput to get the output back as a stream but the whole thing is very slow.

我试图从.NET程序调用php-cgi.exe。我使用RedirectStandardOutput将输出作为流返回,但整个过程非常慢。

Do you have any idea on how I can make that faster? Any other technique?

你对我如何更快地做到这一点有什么想法吗?还有其他技术吗?

    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()

3 个解决方案

#1


8  

You can use the OutputDataReceived event to receive data as it's pumped to StdOut.

您可以使用OutputDataReceived事件在将数据泵送到StdOut时接收数据。

#2


16  

The best solution I have found is:

我发现的最佳解决方案是:

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);
    }
}

#3


2  

The problem is due a bad php.ini config. I had the same problem and i downloaded the Windows installer from: http://windows.php.net/download/.

问题是由于php.ini配置错误。我遇到了同样的问题,我从http://windows.php.net/download/下载了Windows安装程序。

After that and commenting out not needed extensions, the conversion process is alà Speedy Gonzales, converting 20 php per second.

在那之后并评论出不需要的扩展,转换过程是alàSpeedyGonzales,每秒转换20个PHP。

You can safely use "oProcess.StandardOutput.ReadToEnd()". It's more readable and alomost as fast as using the thread solution. To use the thread solution in conjunction with a string you need to introduce an event or something.

您可以安全地使用“oProcess.StandardOutput.ReadToEnd()”。它比使用线程解决方案更快,更快速,更快速。要将线程解决方案与字符串结合使用,您需要引入事件或其他内容。

Cheers

#1


8  

You can use the OutputDataReceived event to receive data as it's pumped to StdOut.

您可以使用OutputDataReceived事件在将数据泵送到StdOut时接收数据。

#2


16  

The best solution I have found is:

我发现的最佳解决方案是:

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);
    }
}

#3


2  

The problem is due a bad php.ini config. I had the same problem and i downloaded the Windows installer from: http://windows.php.net/download/.

问题是由于php.ini配置错误。我遇到了同样的问题,我从http://windows.php.net/download/下载了Windows安装程序。

After that and commenting out not needed extensions, the conversion process is alà Speedy Gonzales, converting 20 php per second.

在那之后并评论出不需要的扩展,转换过程是alàSpeedyGonzales,每秒转换20个PHP。

You can safely use "oProcess.StandardOutput.ReadToEnd()". It's more readable and alomost as fast as using the thread solution. To use the thread solution in conjunction with a string you need to introduce an event or something.

您可以安全地使用“oProcess.StandardOutput.ReadToEnd()”。它比使用线程解决方案更快,更快速,更快速。要将线程解决方案与字符串结合使用,您需要引入事件或其他内容。

Cheers