反应式扩展 (Rx) 错过事件

发布于 2024-11-06 06:48:03 字数 602 浏览 0 评论 0原文

为什么 rx 观察事件会错过?该事件在发生时不会被处理,因此对象的内部状态不会更新,并会导致收到的后续事件出现问题。 是不是ObserveOn NewThread的原因?

private void UpdateList(Client client)
{
    var listUpdateReceive = Observable
        .FromEvent<ListEventArgs>(ev => client.ListUpdateReceive += ev, ev => client.ListUpdateReceive -= ev);

    listUpdateReceive.Take(1)
        .Subscribe(r =>
                       {
                           TraceInformation("List is updated.");

                           OnListUpdateReceived(r.Sender, r.EventArgs);
                       });
}

我可以看到事件已收到,但上面的代码被阻止了!

Why does a rx observing an event missed? The event is not handled at the time it occurs so the inner state of the object is not updated and causes problems in the following events received.
Can it be due to ObserveOn NewThread?

private void UpdateList(Client client)
{
    var listUpdateReceive = Observable
        .FromEvent<ListEventArgs>(ev => client.ListUpdateReceive += ev, ev => client.ListUpdateReceive -= ev);

    listUpdateReceive.Take(1)
        .Subscribe(r =>
                       {
                           TraceInformation("List is updated.");

                           OnListUpdateReceived(r.Sender, r.EventArgs);
                       });
}

I can see the event is received but the code above is blocking!

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

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

发布评论

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

评论(1

零度° 2024-11-13 06:48:03

您的代码示例对我来说看起来很好,尽管使用 Take(1) 运算符将导致仅捕获第一个事件,此时流将完成,并且您将不会收到任何其他通知。您是否打算只监听单个事件通知?

也许删除 Take(1) 会给你正确的行为?

private void UpdateList(Client client)
{
    var listUpdateReceive = Observable
        .FromEvent<ListEventArgs>(ev => client.ListUpdateReceive += ev, ev => client.ListUpdateReceive -= ev);

    listUpdateReceive.Subscribe(r =>
        {
           TraceInformation("List is updated.");

           OnListUpdateReceived(r.Sender, r.EventArgs);
        });
}

Your code sample looks fine to me, although the use of the Take(1) operator will result in only ever catching the first event at which point the stream will complete and you will not receive any additional notifications. Is it your intention to only listen for a single event notification?

Perhaps removing Take(1) will give you the correct behaviour?

private void UpdateList(Client client)
{
    var listUpdateReceive = Observable
        .FromEvent<ListEventArgs>(ev => client.ListUpdateReceive += ev, ev => client.ListUpdateReceive -= ev);

    listUpdateReceive.Subscribe(r =>
        {
           TraceInformation("List is updated.");

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