控制台应用程序不会定期刷新输出

时间:2011-02-06 21:41:13

标签: c# console-application redirectstandardoutput

我正在使用第三方控制台应用程序,该应用程序会定期逐行将数据输出到控制台。当我试图通过我的应用程序运行它以便我可以解析输出数据时,我注意到OutPutstream只有在应用程序退出后才可读。

我使用C#控制台应用程序测试了我的应用程序,该应用程序每隔5秒向控制台输出一些内容,并且它按预期工作。我正在调用的第三方进程是用Java或C ++编写的(不确定),但似乎它可能不符合.NET对控制台应用程序的预期标准。

是否有其他方法可以读取控制台应用程序的数据输出?

编辑:我正在从WPF应用程序调用该进程。因此需要异步读取。

编辑2:控制台应用程序从USB设备(加速度计 - http://www.gcdataconcepts.com/)读取数据。

以下是我使用的代码:

    public void RunProcess()
    {
        Process process = new Process();
        process.StartInfo.FileName = "consoleApp.exe";

        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
        process.Start();
        process.BeginOutputReadLine();
    }

    private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        if (!string.IsNullOrEmpty(outLine.Data))
        {
            Dispatcher.Invoke(new Action(() =>
            {
                textBlock1.Text += outLine.Data + Environment.NewLine;
            }), System.Windows.Threading.DispatcherPriority.Normal);
        }
    }

3 个答案:

答案 0 :(得分:1)

protected virtual void StartProcess() {
        // Start a new process for the cmd
        process = new Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.FileName = FileName;
        process.StartInfo.Arguments = Arguments;
        process.StartInfo.WorkingDirectory = WorkingDirectory;
        process.Start();

        // Invoke stdOut and stdErr readers - each
        // has its own thread to guarantee that they aren't
        // blocked by, or cause a block to, the actual
        // process running (or the gui).
        new MethodInvoker(ReadStdOut).BeginInvoke(null, null);
        new MethodInvoker(ReadStdErr).BeginInvoke(null, null);

    }

    /// <summary>
    /// Handles reading of stdout and firing an event for
    /// every line read
    /// </summary>
    protected virtual void ReadStdOut() {
        string str;
        while ((str = process.StandardOutput.ReadLine()) != null)
        {
            FireAsync(StdOutReceived, this, new DataReceivedEventArgs(str));
        }
    }

    /// <summary>
    /// Handles reading of stdErr
    /// </summary>
    protected virtual void ReadStdErr() {
        string str;
        while ((str = process.StandardError.ReadLine()) != null)
        {
            FireAsync(StdErrReceived, this, new DataReceivedEventArgs(str));
        }
    }

答案 1 :(得分:0)

你也可以这样做:

public void RunProcess()
{
    Process process = new Process();
    process.StartInfo.FileName = "consoleApp.exe";

    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;
    process.Start();

    for (; ; )
    {
        string line = process.StandardOutput.ReadLine();
        if (line == null)
            break;

        Dispatcher.Invoke(new Action(() =>
            {
                textBlock1.Text += outLine.Data + Environment.NewLine;
            }), System.Windows.Threading.DispatcherPriority.Normal);
    }
    ...
}

答案 2 :(得分:0)

更简单的方法是在StandardOutput对象上使用process对象。示例代码:

Process process = new Process();
process.StartInfo.FileName = @"StackOverflowTest.exe";

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();

while (!process.StandardOutput.EndOfStream) 
{
    Console.WriteLine("got: " + process.StandardOutput.ReadLine());
}
相关问题