需要 RX 方面的帮助:将 System.Diagnostics.Process.OutputDataReceived 转换为 Observable

发布于 2024-11-07 07:11:46 字数 738 浏览 0 评论 0原文

我想启动一个子进程并观察它的重定向输出。那不是 C# 对我来说是一个问题,但我尝试理解 RX,所以游戏开始了...... 我有一个用于进程的静态扩展方法,如下所示:

    public static IObservable<IEvent<DataReceivedEventArgs>> GetOutput(this Process that)
    {
        return Observable.FromEvent<DataReceivedEventArgs>(that, "OutputDataReceived");
    }

我创建一个可观察对象并像这样订阅它:

    Process p = ........
    var outObs = p.GetOutput();
    var outSub = outObs.Subscribe(data => Console.WriteLine(data));

这并不是完全错误,但我得到:

System.Collections.Generic.Event`1[System.Diagnostics.DataReceivedEventArgs]

虽然我期望获得字符串:-(

所以,我认为,我的扩展方法返回错误的类型。 如果有人能给我解释一下,那就太好了,什么是 Wong 与我的扩展方法签名。

多谢, ++马布拉

I want to start a subprocess and watch it's redirected output. That not
a problem for me in C#, but I try to understand RX, so the game begins ...
I have a static extension method for process, which looks like this:

    public static IObservable<IEvent<DataReceivedEventArgs>> GetOutput(this Process that)
    {
        return Observable.FromEvent<DataReceivedEventArgs>(that, "OutputDataReceived");
    }

I create an observable and subscribe to it like this:

    Process p = ........
    var outObs = p.GetOutput();
    var outSub = outObs.Subscribe(data => Console.WriteLine(data));

This is not completely wrong, but I am getting:

System.Collections.Generic.Event`1[System.Diagnostics.DataReceivedEventArgs]

while I am expecting to get strings :-(

So, I think, my extensionmethod returns the wrong type.
It would be really good, if someone could explain me, what's
wong with my extension methods signature.

Thanks a lot,
++mabra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-11-14 07:11:46

所以,我认为我的扩展方法返回了错误的类型

,就是这样。

IEvent 包装了传统事件委托的 senderEventArgs 参数。因此,您需要修改代码,使其看起来像这样:

public static IObservable<string> GetOutput(this Process that)
{
    return Observable.FromEvent<DataReceivedEventArgs>(that, "OutputDataReceived")
                     .Select(ep => ep.EventArgs.Data);
}

如果您使用的是最新的 Rx,那么代码有点不同,

public static IObservable<string> GetOutput(this Process that)
{
       return Observable.FromEventPattern<DataReceivedEventArgs>(that, "OutputDataReceived")
                        .Select(ep => ep.EventArgs.Data);
}

这里的关键是 Select 中的 EventArgs EventPattern/IEvent,然后获取Data

So, I think, my extensionmethod returns the wrong type

That's exactly it.

IEvent wraps both the sender and the EventArgs parameters of a tradition Event delegate. So you need to modify your code to look something like

public static IObservable<string> GetOutput(this Process that)
{
    return Observable.FromEvent<DataReceivedEventArgs>(that, "OutputDataReceived")
                     .Select(ep => ep.EventArgs.Data);
}

If you're using the latest Rx, then the code is a bit different

public static IObservable<string> GetOutput(this Process that)
{
       return Observable.FromEventPattern<DataReceivedEventArgs>(that, "OutputDataReceived")
                        .Select(ep => ep.EventArgs.Data);
}

the key here is to Select the EventArgs from the EventPattern/IEvent, and then grab the Data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文