为什么TakeLast< T>()方法不是在ReplaySubject&LT工作; T> [英] Why does TakeLast<T>() method not work on a ReplaySubject<T>

查看:163
本文介绍了为什么TakeLast< T>()方法不是在ReplaySubject&LT工作; T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记载的MSDN文档,以下code应该输出'5'到控制台窗口。相反,不显示任何内容。

According the the MSDN documentation, the following code should output '5' to the console window. Instead, nothing is displayed.

static void Main(string[] args)
{
     var o = new ReplaySubject<int>();

     o.OnNext(0);
     o.OnNext(1);
     o.OnNext(2);
     o.OnNext(3);
     o.OnNext(4);
     o.OnNext(5);

     o.TakeLast(1).Subscribe(Console.WriteLine);

     Console.WriteLine("Press any key to exit");
     Console.ReadKey();
}

期望的输出:

Expected output:

5
Press any key to exit

实际输出:

Press any key to exit

任何人能请解释一下为什么是这样的话?

Can anyone please explain why this is the case?

推荐答案

那是因为你从来没有通知完成序列,所以 TakeLast 不知道序列完整并继续等待该序列的末端。这个工程预期​​:

That's because you never notify the completion of the sequence, so TakeLast doesn't know the sequence is complete and continues to wait for the end of the sequence. This works as expected:

var o = new ReplaySubject<int>();

o.OnNext(0);
o.OnNext(1);
o.OnNext(2);
o.OnNext(3);
o.OnNext(4);
o.OnNext(5);
o.OnCompleted();

o.TakeLast(1).Subscribe(Console.WriteLine);

这篇关于为什么TakeLast&LT; T&GT;()方法不是在ReplaySubject&LT工作; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆