反应式扩展 (Rx) 错过事件
为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码示例对我来说看起来很好,尽管使用
Take(1)
运算符将导致仅捕获第一个事件,此时流将完成,并且您将不会收到任何其他通知。您是否打算只监听单个事件通知?也许删除 Take(1) 会给你正确的行为?
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?