使用冷 observable 处理 OnCompleted
在 Rx 中,以下代码执行以下操作:似乎没有调用我的 OnCompleted 操作?
没有“序列完成”
static void Main(string[] args)
{
var list = new List<int> { 1, 2, 3 };
var obs = list.ToObservable();
IDisposable subscription = obs.SubscribeOn(Scheduler.NewThread).Subscribe(p =>
{
Console.WriteLine(p.ToString());
Thread.Sleep(200);
},
p => Console.WriteLine("Sequence completed"));
Console.ReadLine();
subscription.Dispose();
}
我是否在做一些愚蠢的事情,因为控制台窗口中的 3 之后没有打印“序列完成”?
控制台输出
1
2
3
_
因此,我的问题的主要焦点是如何在迭代此类序列后运行一些代码?
- 例如,在观察到原始列表中的所有元素后,如何执行
Console.WriteLine("Sequencecompleted"))
?观察? - 请注意,
.ToObservable
源自IEnumerable
(本例中为List<>
), - 并且订阅在
上运行>新建线程
in Rx, the following code does not seem to call my OnCompleted action?
No "Sequence Completed"
static void Main(string[] args)
{
var list = new List<int> { 1, 2, 3 };
var obs = list.ToObservable();
IDisposable subscription = obs.SubscribeOn(Scheduler.NewThread).Subscribe(p =>
{
Console.WriteLine(p.ToString());
Thread.Sleep(200);
},
p => Console.WriteLine("Sequence completed"));
Console.ReadLine();
subscription.Dispose();
}
Am i doing something silly, as there is no "Sequence Completed" printed after 3 in the Console window?
Console Output
1
2
3
_
So, the prime focus of my question is how to run some code after this type of sequence has been iterated?
- E.g. how to execute
Console.WriteLine("Sequence completed"))
after all elements in the original list have been observed? - Please note that the
.ToObservable
originated from anIEnumerable
(aList<>
in this case) - And the subscription is run on a
NewThread
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 .Subscribe 的第二个参数是错误回调。仅当观察元素时出现错误时,才会打印“序列完成”字符串。
这是更正后的代码:
The problem is that the 2nd parameter to .Subscribe is the error callback. Your "sequence completed" string would be printed only if there was an error observing the elements.
Here's the corrected code: