为什么TakeLast< T>()方法不是在ReplaySubject&LT工作; T>是在、方法、工作、LT

由网友(拥怀闭眼吻)分享简介:记载的MSDN文档,以下code应该输出'5'到控制台窗口。相反,不显示任何内容。According the the MSDN documentation, the following code should output '5' to the console window. Instead, nothing is...

记载的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);
阅读全文

相关推荐

最新文章