I am currently trying to wrap my head around concurrency using RX.NET and confusing something. I want to run four relatively slow tasks in parallel, so I suggested that NewThreadScheduler.Default would be the way to go, since it "Represents an object that schedules each unit of work in a separate thread."
Here is my installation code:
static void Test() { Console.WriteLine("Starting. Thread {0}", Thread.CurrentThread.ManagedThreadId); var query = Enumerable.Range(1, 4); var obsQuery = query.ToObservable(NewThreadScheduler.Default); obsQuery.Subscribe(DoWork, Done); Console.WriteLine("Last line. Thread {0}", Thread.CurrentThread.ManagedThreadId); } static void DoWork(int i) { Thread.Sleep(500); Console.WriteLine("{0} Thread {1}", i, Thread.CurrentThread.ManagedThreadId); } static void Done() { Console.WriteLine("Done. Thread {0}", Thread.CurrentThread.ManagedThreadId); }
I assumed that "X Thread Y" displays a different thread identifier each time, however the actual output is:
Starting. Thread 1 Last line. Thread 1 1 Thread 3 2 Thread 3 3 Thread 3 4 Thread 3 Done. Thread 3
All work is done in the same new thread in sequential order, which I did not expect.
I suppose I'm missing something, but I can't figure that out.
c # concurrency system.reactive
Anders arpi
source share