given a Subject like this:
var input = new Subject<int>();
and subscribers like this:
var observer1 = input
.Subscribe(ev =>
{
Thread.Sleep(1000);
listBox.Items.Add("o1: " + ev.ToString());
});
var observer2 = input
.Subscribe(ev =>
{
Thread.Sleep(1000);
listBox.Items.Add("o2: " + ev.ToString());
});
how can I tweak it so that when I do
Enumerable.Range(0, 1000).ToList().ForEach((i) =>
{
input.OnNext(i);
});
the OnNext is called async but the subscription is synchronized to the UI thread. (It's a WinForms environment)
I have tried
var observer1 = input
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(ev =>
{
Thread.Sleep(1000);
listBox.Items.Add("o1: " + ev.ToString());
});
And I can see that observer1 and observer2 immediately start their subscribe action without waiting, due to the ObserveOn. But now the app crashes due to the fact that the listbox is called on the non-ui thread. As soon as I sync the action of the subscribe with the UI tread I loose the parallel processing of the OnNext call.
Any idea how to fix it?