开发者

Reactive Extensions (Rx) Misses event

开发者 https://www.devze.com 2023-03-03 22:14 出处:网络
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.

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 Obser开发者_StackOverflow社区veOn 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!


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);
        });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号