Take the 2-minute tour ×

As I understand Subscribe method should be asynchronous whereas Run is synchronous. But this piece of code is working in synchronous manner. Can anybody fix it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive.Linq;

namespace RxExtensionsDemo
    class Program
        static void Main(string[] args)
            IObservable<int> source = Observable.Generate<int, int>(0, i => i < 10000, i => i + 1, i => i * i);

            IDisposable subscription = source.Subscribe(x => { Console.WriteLine("Received {0} from source", x); }, ex =>
                Console.WriteLine("Error occured");

            }, () =>
                Console.WriteLine("Source said there are no more messages to follow");



I always see Asynchronous written to console at the last.

share|improve this question
I don't think it's guaranteed that Subscribe is always async. –  CodesInChaos Sep 8 '11 at 10:51
and even if it is there's no guarantee that one thread will yield at any specific time –  Rune FS Sep 8 '11 at 10:52
I figured that all I was missing was specifying Scheduler option: Scheduler.ThreadPool as the last one. Specifying that did the job :) –  Jaggu Sep 8 '11 at 10:56
add comment

1 Answer

up vote 3 down vote accepted

By default Observable.Generate uses Scheduler.CurrentThread. However, you can specify a different scheduler to get the desired asynchronous behavior:

  i => i < 10000,
  i => i + 1,
  i => i * i,

The Scheduler class is in the System.Reactive.Concurrency namespace.

Other possible asynchronous predefined schedulers are Scheduler.TaskPool and Scheduler.ThreadPool.

share|improve this answer
add comment

Your Answer


