Can’t understand, how scheduler works in Rx Framework. I run code, mention below and recived that “Select thread” and “Subscribe thread” produces same values, and main thread produces antoher value. I thought that “main thread” and “subscribe thread” must have the same value, and “select thread” must have another.
var obs = Observable.Range(1, 10)
.SubscribeOn(Scheduler.ThreadPool)
.ObserveOn(Scheduler.CurrentThread);
var data = obs.Select(x =>
{
Console.WriteLine("Select thread: {0}", Thread.CurrentThread.ManagedThreadId);
return x;
});
data.Subscribe(x => Console.WriteLine("Subscribe thread: {0}", Thread.CurrentThread.ManagedThreadId));
Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);
You’ve made the same mistake that I think everyone does when first looking at the schedulers – I certainly did.
The mistake is thinking that
Scheduler.CurrentThreaduses the thread that defined the observable – instead it is the thread that is executing the observable.So the
Selectis only called when the observable is subscribed to and since you’ve subscribed to it on theThreadPoolit will also be observed on the same thread (CurrentThread).