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

标签 c# console-application redirectstandardoutput

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

我使用 C# 控制台应用程序测试了我的应用程序,该应用程序每 5 秒向控制台输出一些内容,并且按预期工作。我调用的第 3 方进程是用 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);
        }
    }

最佳答案

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

关于c# - 控制台应用程序不会定期刷新输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4916348/

相关文章:

c# - MVC C# 中的下拉列表

c# - 将 Razor 与 Jquery 合并以实现自动完成功能

objective-c - 如何在 Objective C 中编写日志文件?

linux - Fortran 90 不写入标准输出?

c# - C# .NET 位图的最大分辨率是多少?

c# - Visual C#、Winforms 和部分类疯狂

ocaml - 重定向标准输出 OCaml

encoding - 在 Powershell 中,如何选择字符串的不同部分并将它们分别输出到变量并在行中显示?

c++ - 将 stringstream 传递到 unicode 项目中的控制台输出

c# - 发布 C# 控制台应用程序