In your code above, the line m_numbers.ToObservable()
creates an observable sequence that iterates through all the values in m_numbers straight away and then calls OnCompleted
. As there are no values there when it's called at first, nothing happens. You can see this by adding a System.Threading.Thread.Sleep(3100)
before you call subscribe. This will print out three numbers.
A better approach is to use one of Rx's built in methods for creating sequences. One way to get a stream of random numbers one second apart is to use Observable.Interval:
static void Main(string[] args)
{
var random = new Random();
var source = Observable
.Interval(TimeSpan.FromSeconds(1))
.Select(_ => random.Next(1, 100));
source.Subscribe(Console.WriteLine);
Console.ReadLine();
}
The select function ignores the value from Interval
(which is just a counter) and returns a random one.