I have a problem getting my observers working properly when on specific threads.
Subject<bool> subjEvent = new Subject<bool>();
Subject<int> subjValue = new Subject<int>();
IScheduler sched = new EventLoopScheduler(ts => new Thread(ts));
subjEvent.ObserveOn(sched).Subscribe(
r =>
{
if(r)
{
Console.WriteLine("Connected On: \t{0}", Thread.CurrentThread.ManagedThreadId);
subjValue.ObserveOn(sched).Subscribe(
x => Console.WriteLine("Recieved On: \t{0}", Thread.CurrentThread.ManagedThreadId));
}else{
Console.WriteLine("Disconnect On: \t{0}", Thread.CurrentThread.ManagedThreadId);
}
}
);
subjEvent.OnNext(true);
for(int i=0; i< 10; i++)
{
subjValue.OnNext(i);
}
subjEvent.OnNext(false);
subjValue.OnCompleted();
subjEvent.OnCompleted();
The idea is to subscribe to something when it becomes available and unsubscribe/resubscribe with events thereafter. Now as I require to observe on a specific (read same) thread, as well as ensure correct ordering, I have an EventLoopScheduler.
The problem is I get nothing from the value subscription.
Now, if I add a Thread.Sleep(10) to the generation loop (after OnNext) it works perfectly fine. So I'm kinda perplexed as to what I'm doing wrong and would be very grateful for help/advise.