小编Eld*_*dar的帖子

为什么Rx Observable.Subscribe阻止我的线程?

您好'我已经尝试了101个Rx示例中的一个:

    static IEnumerable<int> GenerateAlternatingFastAndSlowEvents()
    {
        int i = 0;

        while (true)
        {
            if (i > 1000)
            {
                yield break;
            }
            yield return i;
            Thread.Sleep(i++ % 10 < 5 ? 500 : 1000);
        }
    }

    private static void Main()
    {
        var observable = GenerateAlternatingFastAndSlowEvents().ToObservable().Timestamp();
        var throttled = observable.Throttle(TimeSpan.FromMilliseconds(750));

        using (throttled.Subscribe(x => Console.WriteLine("{0}: {1}", x.Value, x.Timestamp)))
        {
            Console.WriteLine("Press any key to unsubscribe");
            Console.ReadKey();
        }

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么"按任意键取消订阅"这一行从未显示过.我的理解是订阅是异步的,你订阅它并立即返回.我错过了什么导致我的主线程被阻止?

system.reactive observable

4
推荐指数
1
解决办法
3721
查看次数

标签 统计

observable ×1

system.reactive ×1