重定向标准输出,不引发事件 [英] redirecting standard output, event is not raised

查看:169
本文介绍了重定向标准输出,不引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动一个程序并读取它的标准输出.但是,该事件从未引发过. 我启动的进程正在运行,并且在控制台中使用相同的参数创建输出. 有什么想法,我在做什么错了?

I'm trying to start a programm and read it's standard output. However the event is never raised. The process i start is running and in console with the same arguments it creates output. Any ideas, what I'm doing wrong?

    public void StartProcess(string Filename, string Arguments)
    {
        currentProcess = new Process();
        currentProcess.StartInfo.FileName = Programm;
        currentProcess.StartInfo.Arguments = Arguments;
        currentProcess.StartInfo.UseShellExecute = false;
        currentProcess.StartInfo.RedirectStandardOutput = true;
        currentProcess.StartInfo.RedirectStandardError = true;
        currentProcess.OutputDataReceived += OutputReceivedEvent;
        currentProcess.EnableRaisingEvents = true;

        string path = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_Result.txt";
        LastResult = path;
        resultfile = File.CreateText(path);

        currentProcess.Start();
        currentProcess.BeginOutputReadLine();
        response.command = Command.ACK;
        SendMessage(response);

    }
    private void OutputReceivedEvent(object sender, DataReceivedEventArgs e)
    {
        resultfile.WriteLine(e.Data);
    }

我刚刚发现了一些奇怪的事情:我启动的过程是mcast.如果我启动诸如ping之类的东西,我的代码就可以正常工作.因此,mcast似乎做了一些有趣的事情!

I just discoverd something odd: The process I start is mcast. If I start something else like ping, my code works just fine. So mcast seems to do something funny!

所以下面的代码可以正常工作,但是只读取特定大小的块,如果将更少的字节写入流,则偶数不会发生,.ReadBlock也不会返回任何内容.

So the code below is working, but only reads blocks of a certain sizes, if less bytes are written to the stream, the even does not occur, neither does .ReadBlock return anything.

所以还有一个更新,问题是,mcast不会刷新其输出流.我最终编写了自己的工具,效果很好.

So one more update, the problem is, that mcast does not flush it's output stream. I ended up writing my own tool, which is working just fine.

推荐答案

根据进程的不同,您可能还需要先使用currentProcess.ErrorDataReceived,然后再使用currentProcess.BeginErrorReadLine(),因为某些进程只是重定向到一个输出,而不能同时重定向到两个输出.无论如何都值得尝试.

Depending on the process you may also need to use currentProcess.ErrorDataReceived and then currentProcess.BeginErrorReadLine() too, as some processes just redirect to one output, and not both. It's worth trying anyway.

所以它看起来像这样:

public void StartProcess(string Filename, string Arguments)
{
    currentProcess = new Process();
    currentProcess.StartInfo.FileName = Programm;
    currentProcess.StartInfo.Arguments = Arguments;
    currentProcess.StartInfo.UseShellExecute = false;
    currentProcess.StartInfo.RedirectStandardOutput = true;
    currentProcess.StartInfo.RedirectStandardError = true;
    currentProcess.OutputDataReceived += OutputReceivedEvent;
    currentProcess.ErrorDataReceived += ErrorReceivedEvent; 

    currentProcess.EnableRaisingEvents = true;

    string path = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_Result.txt";
    LastResult = path;
    resultfile = File.CreateText(path);

    currentProcess.Start();
    currentProcess.BeginOutputReadLine();
    currentProcess.BeginErrorReadLine();
    response.command = Command.ACK;
    SendMessage(response);

}
private void OutputReceivedEvent(object sender, DataReceivedEventArgs e)
{
    resultfile.WriteLine(e.Data);
}

/*This second event handler is to prevent a lock occuring from reading two separate streams. 
Not always an issue, but good to protect yourself either way. */
private void ErrorReceivedEvent(object sender, DataReceivedEventArgs e)
{
    resultfile.WriteLine(e.Data);
}

这篇关于重定向标准输出,不引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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