C# 控制台应用程序没有';t定期刷新输出

C# 控制台应用程序没有';t定期刷新输出,c#,console-application,redirectstandardoutput,C#,Console Application,Redirectstandardoutput,我使用的是一个第三方控制台应用程序,它定期逐行向控制台输出数据。当我试图通过我的应用程序运行它以便分析输出数据时,我注意到只有在应用程序退出后,OutPutstream才是可读的 我用一个C#控制台应用程序测试了我的应用程序,该应用程序每5秒向控制台输出一次数据,并且工作正常。我所调用的第三方进程要么是用java编写的,要么是用C++编写的(不确定),但是它似乎不符合.NET对控制台应用程序的期望。 是否有其他方法读取控制台应用程序输出的数据 Edit:我正在从WPF应用程序调用该进程。因此需要

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

我用一个C#控制台应用程序测试了我的应用程序,该应用程序每5秒向控制台输出一次数据,并且工作正常。我所调用的第三方进程要么是用java编写的,要么是用C++编写的(不确定),但是它似乎不符合.NET对控制台应用程序的期望。 是否有其他方法读取控制台应用程序输出的数据

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

编辑2:控制台应用程序从USB设备(加速计-)读取数据

下面是我使用的代码:

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

您也可以这样做:

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

更简单的方法是在
进程
对象上使用
标准输出
对象。示例代码:

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());
}
受保护的虚拟void StartProcess(){
//启动cmd的新进程
流程=新流程();
process.StartInfo.UseShellExecute=false;
process.StartInfo.RedirectStandardOutput=true;
process.StartInfo.RedirectStandardError=true;
process.StartInfo.CreateNoWindow=true;
process.StartInfo.FileName=文件名;
process.StartInfo.Arguments=参数;
process.StartInfo.WorkingDirectory=WorkingDirectory;
process.Start();
//调用stdOut和stdErr读取器-每个
//有自己的线程来保证它们不会
//被实际用户阻塞或导致阻塞
//正在运行的进程(或gui)。
新的MethodInvoker(ReadStdOut).BeginInvoke(null,null);
新的MethodInvoker(ReadStdErr).BeginInvoke(null,null);
}
/// 
///处理标准输出的读取并为其触发事件
///每一行都读
/// 
受保护的虚拟void ReadStdOut(){
字符串str;
while((str=process.StandardOutput.ReadLine())!=null)
{
FireAsync(StdOutReceived,this,newdatareceivedeventargs(str));
}
}
/// 
///处理stdErr的读取
/// 
受保护的虚拟void ReadStdErr(){
字符串str;
while((str=process.StandardError.ReadLine())!=null)
{
FireAsync(StdErrReceived,this,newdatareceivedeventargs(str));
}
}

不工作。同样的行为。我只在设置
CreateNoWindow=false
并手动关闭窗口时才看到输出。不工作。同样的行为。我只在设置
CreateNoWindow=false
并手动关闭窗口时看到输出。@Omar-什么意思“我只在…”时看到输出。对于这种代码,您不应该看到输出,这是它应该工作的方式,因为它是重定向的。或者我错过了什么?我的意思是我只能从生成的控制台看到输出,而不能在创建流程的应用程序中看到输出。我已经对此进行了修复,您可以在类似的问题上找到它。。。
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));
        }
    }