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

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

问题描述

我的项目有问题.我想启动一个进程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& BeginOutputReadLine
  • StreamWriter

没有任何效果.它总是等待"过程的结束,以显示我想要的东西. 我没有任何代码可放,即使您希望我的代码带有上面列出的内容之一.谢谢.

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

过程"是我的预制过程

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

这是一个有效的示例:

This is a working example:

        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)\gnuwin32\bin\ls.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.

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

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