使用冷 observable 处理 OnCompleted

发布于 2024-12-07 02:31:07 字数 1040 浏览 0 评论 0原文

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 an IEnumerable (a List<> in this case)
  • And the subscription is run on a NewThread

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中性美 2024-12-14 02:31:08

问题是 .Subscribe 的第二个参数是错误回调。仅当观察元素时出现错误时,才会打印“序列完成”字符串。

这是更正后的代码:

var list = new List<int> { 1, 2, 3 };
var obs = list.ToObservable();
var subscription = obs.SubscribeOn(Scheduler.NewThread).Subscribe(p =>
    {
        Console.WriteLine(p.ToString());
        Thread.Sleep(200);
    },
    error => Console.WriteLine("Error!"),
    () => Console.WriteLine("sequence completed"));
Console.ReadLine();
subscription.Dispose();

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:

var list = new List<int> { 1, 2, 3 };
var obs = list.ToObservable();
var subscription = obs.SubscribeOn(Scheduler.NewThread).Subscribe(p =>
    {
        Console.WriteLine(p.ToString());
        Thread.Sleep(200);
    },
    error => Console.WriteLine("Error!"),
    () => Console.WriteLine("sequence completed"));
Console.ReadLine();
subscription.Dispose();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文