Observable.FromEvent doesn't complete

本文关键字:complete FromEvent doesn Observable | 更新日期: 2023-09-27 18:28:12

var DeviceSubject = new Subject<NetworkEventArgs>();
Observable.FromEvent<EventHandler<NetworkEventArgs>, NetworkEventArgs>(h => Device.Connected += h, h => Device.Connected -= h)
.Subscribe(DeviceSubject);

设备已连接事件声明如下...

public event EventHandler<NetworkEventArgs> Connected;

我的MainForm_Load方法在调用 Observable.FromEvent 后停止执行。 谁能帮忙?

Observable.FromEvent doesn't complete

尝试调用FromEventPatternFromEvent适用于非标准事件。

var DeviceSubject = new Subject<NetworkEventArgs>();
Observable
    .FromEventPattern<EventHandler<NetworkEventArgs>, NetworkEventArgs>(
        h => Device.Connected += h,
        h => Device.Connected -= h)
    .Select(ep => ep.EventArgs)
    .Subscribe(DeviceSubject);

作为旁注:小心使用这样的主题。这样做是一种常见的反模式。任何可观察到的调用OnComplete到你的主题中都可能杀死你的代码。最好只链接查询。