I have a winform application, and an observable set up like this:
Form form = new Form();
Label lb = new Label();
form.Controls.Add(lb);
Observable.Interval(TimeSpan.FromSeconds(1))
.Subscribe(l => lb.Text = l.ToString());
Application.Run(form);
This doesn't work, since the l => lb.Text = l.ToString()
will not be run on the main thread which created the form, but I cannot figure out how to make it run on this thread. I assume, that I should use IObservable.SubscribeOn
which takes either an IScheduler
or a SynchronizationContext
, but I don't know how to get the synchronizationcontext of the main thread, and the only Schedulers I could find were the static properties of Scheduler
, such as Scheduler.CurrentThread
, Immediate
, NewThread
, TaskPool
and ThreadPool
, none of which worked.
My Rx version is 1.0.10621.
SubscribeOn
only sets which thread the actual subscribing happens on, whereasObserveOn
determines which thread theOnNext
calls get executed. For comparison with events,SubscribeOn
is like making you only add event handlers on the main thread, butObserveOn
makes sure the event handling routine gets called on the right thread. – Rosenda