从 Process 获取实时输出 [英] Get Live output from Process

查看:155
本文介绍了从 Process 获取实时输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有问题.我想启动一个进程,7z.exe(控制台版本).我尝试了三种不同的方法:

I've a problem in my project. I would like to launch a process, 7z.exe (console version). I've tried three different things:

  • Process.StandardOutput.ReadToEnd();
  • OutputDataReceived &开始输出读取行
  • 流写入器

没有任何效果.它总是等待"过程结束以显示我想要的内容.我没有任何代码可以放置,只要您希望我的代码包含上面列出的其中一项内容.谢谢.

Nothing works. It always "wait" for the end of the process to show what i want. I don't have any code to put, just if you want my code with one of the things listed upthere. Thanks.

我的代码:

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

        this.sr = process.StandardOutput;
        while (!sr.EndOfStream)
        {
            String s = sr.ReadLine();
            if (s != "")
            {
                System.Console.WriteLine(DateTime.Now + " - " + s);
            }
        }

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(recieve);
process.StartInfo.CreateNoWindow = true;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
public void recieve(object e, DataReceivedEventArgs outLine)
{
    System.Console.WriteLine(DateTime.Now + " - " + outLine.Data);
}

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = p.StandardOutput.ReadToEnd();
process.WaitForExit();

其中流程"是我的预制流程

Where "process" is my pre-made Process

好的,我知道为什么它不能正常工作:7z.exe 是错误:它在控制台中显示加载百分比,并且仅在当前文件完成时才发送信息.例如,在提取中,它工作正常:).我将寻找另一种方法来使用 7z 函数而不使用 7z.exe(可能使用 7za.exe 或某些 DLL).谢谢大家.为了回答这个问题,OuputDataRecieved 事件工作正常!

Ok i know why it doesn't works properly: 7z.exe is the bug: it display a percent loading in console, and it sends information only when the current file is finished. In extraction for example, it works fine :). I will search for another way to use 7z functions without 7z.exe (maybe with 7za.exe or with some DLL). Thanks to all. To answer to the question, OuputDataRecieved event works fine !

推荐答案

看看这个页面,它看起来是你的解决方案:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspxhttp://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

Take a look at this page, it looks this is the solution for you: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx and http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

这是一个工作示例:

        Process p = new Process();
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = @"C:Program Files (x86)gnuwin32inls.exe";
        p.StartInfo.Arguments = "-R C:\";

        p.OutputDataReceived += new DataReceivedEventHandler((s, e) => 
        { 
            Console.WriteLine(e.Data); 
        });
        p.ErrorDataReceived += new DataReceivedEventHandler((s, e) =>
        {
            Console.WriteLine(e.Data);
        });

        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();

顺便说一句,ls -R C: 以递归方式列出 C: 根目录下的所有文件.这些文件很多,我敢肯定当第一个结果出现在屏幕上时还没有完成.7zip 有可能在显示之前保存输出.我不确定你给过程提供了什么参数.

Btw, ls -R C: lists all files from the root of C: recursively. These are a lot of files, and I'm sure it isn't done when the first results show up in the screen. There is a possibility 7zip holds the output before showing it. I'm not sure what params you give to the proces.

这篇关于从 Process 获取实时输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆