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

发布于 2024-10-16 06:55:50 字数 1323 浏览 4 评论 0原文

我正在使用第三方控制台应用程序,该应用程序定期将数据逐行输出到控制台。当我尝试通过我的应用程序运行它以便可以解析输出数据时,我注意到 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);
        }
    }

I'm using a 3rd party console application that periodically outputs data to the console line by line. When I attempted to run it through my App so that I can parse the output data, I noticed that the OutPutstream was readable only AFTER the application quits.

I tested my application with a C# console application that outputs something to the console every 5 seconds and it's working as expected. The 3rd party process I'm calling is either written in Java or C++ (not sure), but it seems that it might not adhere to the standards that .NET expects of console applications.

Is there another way to read the data output by a console application?

Edit: I'm calling the process from a WPF application. So async reads are required.

Edit 2: The console application reads data from a USB device (an accelerometer - http://www.gcdataconcepts.com/).

Below is the code I used:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-10-23 06:55:50
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));
        }
    }
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));
        }
    }
太傻旳人生 2024-10-23 06:55:50

你也可以这样做:

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

You can also do it like this:

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);
    }
    ...
}
记忆消瘦 2024-10-23 06:55:50

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

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

A simpler way would be to use StandardOutput object on your process object. Example code:

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文