2

I have a reactiveui/rx app. Currently, I'm developing it's desktop part, so it's a wpf application. I would like to create each window in its separate thread, because of performance reasons.

That's why I need ObserveOn to be run on a current thread

someCollection.Changed.Buffer(TimeSpan.FromSeconds(n)).ObserveOn(*should be a link to a current thread*).Subscribe(bufferedItems => {})

also, this code is located in a portable lib, that's why I can't use System.Threading.Thread class

Is there any workaround?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Mezk Erei
  • 65
  • 6
  • What's "current thread"? is it the UI thread or is it any random thread? If it's the UI/main thread, you can use the `SynchronizationContext`.Current as a parameter for your `ObserveOn` but if it's any random thread, you probably will have to write your own scheduler (or at least i'm not aware of one that does this). – kha Jun 13 '16 at 13:19
  • I create a window in a new thread so that window object would have a separate dispatcher (according to this https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/). SynchronizationContext.Current wouldn't work, as it will always return null, if it's called not from ui-thread. I already have tried writing my own scheduler, but it's job to create a new Thread, not to reuse the existing one – Mezk Erei Jun 13 '16 at 14:16

1 Answers1

3

If you can capture the Dispatcher you can then provide that to the DispatcherScheduler.

var currentDispatcher = Dispatcher.CurrentDispatcher;
var scheduler = new DispatcherScheduler(currentDispatcher);

Observable.Empty<int>().ObserveOn(scheduler)....
Lee Campbell
  • 10,631
  • 1
  • 34
  • 29