我试图从.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()
有帮助吗?

解决方案

您可以使用 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);
    }
}

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

在此之后并注释掉不需要的扩展程序,转换过程就是à Speedy Gonzales,每秒转换20个PHP。

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

干杯

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top