Reactive Extensions: Why does this exit immediately?

烂漫一生 提交于 2019-12-11 21:26:24

问题


I am reading IntroToRx and I'm having a bit of trouble with the sample code. Here is the sum total of my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LearningReactiveExtensions
{
  public class Program
  {
    static void Main(string[] args)
    {
        var observable = Observable.Interval(TimeSpan.FromSeconds(5));
        observable.Subscribe(
          Console.WriteLine, 
          () => Console.WriteLine("Completed")
        );
        Console.WriteLine("Done");
        Console.ReadKey();
    }

  }
}

If I understand the book correctly, this should write a sequence of numbers to the console, once every five seconds forever since I never Dispose() of the sequence.

However, when I run the code, all I get is the "Done" at the end. No numbers, no "completed", nothing but "Done".

What am I doing wrong here?


回答1:


I am assuming you haven't had the patience to wait for 5 seconds to elapse otherwise you would have seen that the code is working.

The main idea to keep in mind with Rx is that Observable.Subscribe will return control to calling method almost immediately. In other words, Observable.Subscribe does not block until the results are produced. Thus the call to Console.WriteLine will be invoked only after five seconds.




回答2:


You need some way to make the main thread wait for what you are doing. You can use a semaphore if you like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LearningReactiveExtensions
{
  public class Program
  {
    static void Main(string[] args)
    {
         SemaphoreSlim ss = new SemaphoreSlim(1);
        var observable = Observable.Interval(TimeSpan.FromSeconds(5));
        observable.Subscribe(
          Console.WriteLine, 
          () => {
               Console.WriteLine("Completed");
               ss.Release();
          }
        );
        ss.Wait();
        Console.WriteLine("Done");
        Console.ReadKey();
    }

  }
}

Though probably better in this case just to write

  static void Main(string[] args)
   {
        SemaphoreSlim ss = new SemaphoreSlim(1);
        Observable.Interval(TimeSpan.FromSeconds(5)).Wait();
        Console.WriteLine("Completed");
        Console.WriteLine("Done");
        Console.ReadKey();
   }


来源:https://stackoverflow.com/questions/18415558/reactive-extensions-why-does-this-exit-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!