简体   繁体   English

流程执行期间不会刷新重定向标准输出

[英]Redirect standard output isn't flushed during process execution

I have a console application and I want to process the std out in ac# application. 我有一个控制台应用程序,我想在ac#应用程序中处理std。

Basically I already managed to do this with this code: 基本上我已经使用以下代码做到了这一点:

Process ProcessObj = new Process();

ProcessObj.StartInfo.WorkingDirectory = WorkingPath;
ProcessObj.StartInfo.FileName = ApplicationPath;
ProcessObj.StartInfo.Arguments = ApplicationArguments;

ProcessObj.StartInfo.UseShellExecute = false;
ProcessObj.StartInfo.CreateNoWindow = true;

ProcessObj.StartInfo.RedirectStandardOutput = true;

// Start the process
ProcessObj.Start();

// loop through until the job is done
bool stopper = false;
while (!stopper)
{
    stopper = ProcessObj.WaitForExit(100);

    string line = null;

    // handle normal outputs (loop through the lines)
    while (true)
    {
        line = ProcessObj.StandardOutput.ReadLine();
        if (line == null)
            break;

        Logger.Trace("Out: \"" + line + "\"");
    }
}

When the process runs only a few seconds it looks like the whole thing is working without any problem. 当该过程仅运行几秒钟时,看起来整个过程就可以正常工作了。 When I change the configuration of the console application to calculate more, it comes that the process is running for hours. 当我更改控制台应用程序的配置以进行更多计算时,该过程将运行数小时。 In this time my C# application gets no response from the console app. 此时,我的C#应用​​程序没有从控制台应用程序得到响应。 Since the console app is hidden it looks like the app stucked but that's not true. 由于控制台应用程序是隐藏的,因此看起来好像应用程序卡住了,但这不是事实。 It is already running in the background and it seems that all std outputs are only piped through to my c# app when the console app was finished the execution. 它已经在后台运行,并且似乎所有的std输出仅在控制台应用完成执行后才通过管道传递到我的c#应用。 So the problem is, I don't see the std out lines live in my c# app. 所以问题是,我的c#应用程序中没有看到std out行。 It will be refreshed after hours when the console app has finished. 控制台应用程序完成后,将在数小时后刷新。

Is there any way to flush this std out redirection? 有什么方法可以刷新此std重定向? Anybody knows why this isn't working like I want? 有人知道为什么这样不起作用吗?

PS: When I execute the console app as standalone in a normal cmd window the outputs are shown live without any problem. PS:当我在普通cmd窗口中独立执行控制台应用程序时,输出显示为实时显示,没有任何问题。

Please help. 请帮忙。

Try and read the output while the application is running? 尝试在应用程序运行时读取输出? And save it to a buffer? 并将其保存到缓冲区? Then process the output when the application exits. 然后在应用程序退出时处理输出。

pseudo stuff 伪东西

string buffer = string.Empty;
while(!process.HasExited)
{
    string line = process.Stream.ReadLine();
    if (line != null)
        buffer += Enviorment.Newline + line
}

// Do stuff without put here
Console.Write(buffer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM